I’m trying to set up a series of .htacces rules that work like this. My .htacces is inside the doc root.
- rewrite the uri (internally) to be relative to a subdirectory
- if the resource doesn’t exist return a 200 with a specific file.
Here’s what I have so far:
RewriteEngine on
RewriteCond /some/path/%{REQUEST_URI} !-f
RewriteRule ^ /some/path/index.html [L]
RewriteCond %{REQUEST_URI} !^/some/path
RewriteRule (.*) /some/path/$1 [L]
This causes my Apache server to return errors. I understand this is wrong but I don’t know why. I’ve been researching this for a few hours. Also if there’s a cleaner way I can do this please let me know, ty!
To clarify the second rewrite pair works by itself (when it comes to using the subdirectory), it’s when used in combination with the first pair (using a file by default) that if fails. Also I don’t want to use ErrorDocument because it must return a 200 in the missing file case
2
Answers
This is what I ended up going with. I ended up modifying the post virtual hosts include in Apache, and added something like this:
This condition will always be successful (
/some/path/%{REQUEST_URI}
will never map to a file since you need to be testing an absolute file-path here, not a URL-path), so the request is always rewritten to/some/path/index.html
.Try the following instead (in the root
.htaccess
file):Note that, with the current rules, any files outside of
/some/path/
will not be accessible. (Unless perhaps you have additional.htaccess
files in those directories that should be accessible?)