This rule works just fine in a .htaccess file:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteRule ^(index(\.(html|htm))?)$ / [R]
The same rule written as a conditional doesn’t work:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^(index(\.(html|htm))?)$
RewriteRule ^(index(\.(html|htm))?)$ / [R]
I suppose my question is that why is the rule working in the first scenario, but not in the second? And how can I fix it?
I believe your problem is that the
RewriteBaseprocessing (which always strips the leading slash) doesn’t affect variables like%{REQUEST_URI}and doesn’t apply forRewriteCondprocessing anyway.So, the pattern
^(index(\.(html|htm))?)$will work fine in aRewriteRule, but not aRewriteCondmatching against the request URI. You need to include that leading slash (at least if you’re using the beginning^anchor):