.user.ini answers 500 or 418 instead of 404

The symptom

A rule in .htaccess answers 404 for .user.ini, and it works on your own server. On shared hosting the same request answers 500 or 418 with an error page, the rest of the site works, and a test that expects 404 fails.

The usual cause is a block on the hosting: the server refuses this file name before Apache reads your .htaccess. The file stays private, so the site needs no fix. The check does: it should prove that the settings cannot be read instead of expecting one status code.

This manual reproduces the case in a small lab and ends with a test that passes on both servers and still fails when the file is readable. Tools used here: Apache, PHP, curl, Docker, Python and pytest.

Build the lab

You need Docker and uv. The lab is one folder with a PHP page and its .user.ini:

mkdir -p ini-lab/site/demo

cd ini-lab

vi site/demo/.user.ini

; PHP settings for this folder session.name = DEMOSESSID session.cookie_httponly = 1 session.cookie_secure = 1

vi site/demo/index.php

<?php echo "demo page\n";

The official PHP image ignores .htaccess until AllowOverride permits it:

vi lab.conf

<Directory /var/www/html> AllowOverride All </Directory>

Start Apache with mod_rewrite on port 8095 of the loopback interface:

docker run -d --name ini-lab -p 127.0.0.1:8095:80 -v "$PWD/site:/var/www/html:ro" -v "$PWD/lab.conf:/etc/apache2/conf-enabled/lab.conf:ro" php:8.5-apache sh -c "a2enmod rewrite >/dev/null && exec apache2-foreground"

Give it a second to start, then ask for the page:

curl -s http://127.0.0.1:8095/demo/

demo page

Why the file needs protecting

PHP under CGI or FastCGI reads .user.ini files as settings for their folder. To Apache it is an ordinary file. The default configuration denies names that start with .ht, and this name does not:

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/demo/.user.ini

200

curl -s http://127.0.0.1:8095/demo/.user.ini

; PHP settings for this folder session.name = DEMOSESSID session.cookie_httponly = 1 session.cookie_secure = 1

For comparison, the same server refuses .htaccess:

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/.htaccess

403

On a real site this file can show paths, limits and session settings to anyone who asks for it.

Block it with your own rule

vi site/.htaccess

RewriteEngine On RewriteRule (^|/)\.user\.ini$ - [R=404,L]

(^|/) matches the file in the root and in every subfolder, - keeps the URL, R=404 sets the status and L stops the rules. Apache reads the file on the next request, so no restart is needed:

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/demo/.user.ini

404

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/demo/

200

A Files block protects the file too, and answers 403 instead:

<Files ".user.ini"> Require all denied </Files>

Reproduce the hosting block

A host can refuse a file name in its own server configuration, which runs before .htaccess. To see the effect, replace the default virtual host of the lab with one that has such a rule. The lab uses 500 because mod_rewrite does not accept R=418:

vi host-block.conf

<VirtualHost *:80> DocumentRoot /var/www/html RewriteEngine On RewriteRule "\.user\.ini$" - [R=500,L] </VirtualHost>

Recreate the container with this file. Your .htaccess stays in place:

docker rm -f ini-lab

docker run -d --name ini-lab -p 127.0.0.1:8095:80 -v "$PWD/site:/var/www/html:ro" -v "$PWD/lab.conf:/etc/apache2/conf-enabled/lab.conf:ro" -v "$PWD/host-block.conf:/etc/apache2/sites-enabled/000-default.conf:ro" php:8.5-apache sh -c "a2enmod rewrite >/dev/null && exec apache2-foreground"

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/demo/.user.ini

500

Your 404 rule did not run: the request ended before Apache looked for .htaccess. The answer is an error page with none of the settings in it:

curl -s http://127.0.0.1:8095/demo/.user.ini | grep -c "session\."

0

curl -s http://127.0.0.1:8095/demo/.user.ini | grep -i "<title>"

<title>500 Internal Server Error</title>

A real host may also change the status with the HTTP version. On a DreamHost shared account in November 2026, the same request answered as below, both times with a 500 Internal Server Error page:

curl -s --http1.1 -o /dev/null -w "%{http_code}\n" https://example.com/demo/.user.ini

500

curl -s --http2 -o /dev/null -w "%{http_code}\n" https://example.com/demo/.user.ini

418

Tell a host block from a broken rule

Ask for a .user.ini in a folder that does not exist, then for another missing file:

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/no-such-folder/.user.ini

500

curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:8095/demo/no-such-file.txt

404

The same four requests on the two setups of the lab:

RequestYour rule answersThe host block answers
a real .user.ini 404 500
.user.ini in a folder that does not exist 404 500
another missing file 404 404
a normal page 200 200

When a .user.ini that does not exist gets an error and other missing files get 404, the name itself is blocked. If normal pages answer 500 too, look for a broken line in .htaccess instead: the php_flag 500 is a common one.

Fix the check

The test reads the settings from your local copy and fails if any of them appears in the response. Only then does it look at the status: exactly 404 on your own server, any refusal when HOSTED is set:

vi test_user_ini.py

"""The per-folder PHP settings file must never be served.""" import os from pathlib import Path import requests BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8095").rstrip("/") LOCAL_FILE = Path("site/demo/.user.ini") # Your own server runs your rule. A hosting block may answer before it. ALLOWED = {403, 404, 418, 500} if os.environ.get("HOSTED") else {404} def settings(): lines = (line.strip() for line in LOCAL_FILE.read_text().splitlines()) return [line for line in lines if line and not line.startswith(";")] def test_user_ini_is_not_served(): response = requests.get( f"{BASE_URL}/demo/.user.ini", timeout=10, allow_redirects=False ) leaked = [line for line in settings() if line in response.text] assert not leaked, f"the file is readable: {leaked}" assert response.status_code in ALLOWED, f"status {response.status_code}"

uvx runs pytest in a temporary environment with requests. Without HOSTED, the blocked lab fails just as the original check did:

uvx --with requests pytest -q --tb=no -rf test_user_ini.py

F [100%] ========================================== short test summary info =========================================== FAILED test_user_ini.py::test_user_ini_is_not_served - AssertionError: status 500 1 failed in 0.12s

With HOSTED=1 the refusal is accepted:

HOSTED=1 uvx --with requests pytest -q --tb=no -rf test_user_ini.py

. [100%] 1 passed in 0.13s

Now check that the test still protects you. Take the rule away, start the server without the block, and run it in the same relaxed mode:

mv site/.htaccess rule.off

docker rm -f ini-lab

docker run -d --name ini-lab -p 127.0.0.1:8095:80 -v "$PWD/site:/var/www/html:ro" -v "$PWD/lab.conf:/etc/apache2/conf-enabled/lab.conf:ro" php:8.5-apache sh -c "a2enmod rewrite >/dev/null && exec apache2-foreground"

HOSTED=1 uvx --with requests pytest -q --tb=no -rf test_user_ini.py

F [100%] ========================================== short test summary info =========================================== FAILED test_user_ini.py::test_user_ini_is_not_served - AssertionError: the file is readable: ['session.name... 1 failed in 0.13s

Put the rule back and remove the lab:

mv rule.off site/.htaccess

docker rm -f ini-lab

What to keep in mind

Article author: Klavdiya Kondakova