I have the following rewrite rule which converts example.com/anything/
to load the page at example.com/anything.html
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.html [L]
I need an exception to this rule when a physical directory or file exists. For example, if there is a physical directory at example.com/test/
then it should load an index from the /test/
directory, instead of rewriting it to example.com/test.html
I believe I need rewrite conditions RewriteCond
with !-d
and !-f
similar to:
https://httpd.apache.org/docs/2.4/rewrite/remapping.html#fallback-resource
I'm having trouble figuring out how to write these conditions.
Solution:
Prefixing the condition:
RewriteCond %{REQUEST_FILENAME} !-d
Allows directories to be read as they usually would. Also my regex pattern match doesn't consider file names with extensions (lacking a dot .
), so files such as example.css
will be read as usual also.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.html [L]