Arthur Araújo Arthur Araújo - 3 months ago 9
Apache Configuration Question

Apache is redirecting folder to file .html with the same name

I'm trying to redirect 404 json fales to 404.json (with "{}" as content). But apache is redirecting folder to a html file with the same folder name and any custom 404.json redirect fails.

File structure:


  • /example1/

  • /example2/

  • /example1.html

  • /example2/data.json

  • /data.json



Working (redirect to 404.html):



Working (redirect to 404.json):



Working (open the file):



Not working (default apache 404):



It seems apache is redirecting "example1/" to "example1.html", especially when I try to access a file inside the directory like http://example.com/example2/data.json, shows 404 message:


The requested URL /example2.html/data.json was not found on this server.


It happens even without the .htaccess! All the problem because the example1.html file.

htaccess:

# Rewrite MOD
Options +FollowSymLinks
RewriteEngine On

# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]

# 404 json
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)\.json$ 404.json [L]

# 404 page
RewriteRule . 404.html [L]


Maybe it is some apache file mapping configuration but I can't find anything about it. It happens only for deploy server, for localhost it's fine. =/

Answer

Have it like this with MultiViews turned off:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 404 json
RewriteRule \.json$ 404.json [L,NC]

# 404 page
RewriteRule ^ 404.html [L]