Apache .htaccess - If directive to evaluate if home is the current page
This is most probably the result of a conflict with mod_dir and the DirectoryIndex
. (Or a standard internal rewrite, if you are using a front-controller pattern - although that wouldn't normally apply in this instance.)
<If>
expressions are merged late, after mod_dir has already rewritten the request (in the form of an internal subrequest) to the directory index document, eg. /index.html
. The REQUEST_URI
server variable is updated to reflect the rewritten URL-path.
So, you either need to check for the directory index document:
<If "%{REQUEST_URI} =~ m#^/index\.html$#">
#some other rules...
Header always set Test "It works"
</If>
OR, check against THE_REQUEST
server variable instead, which contains the first line of the request headers and does not change when the request is internally rewritten.
THE_REQUEST
will contain a string of the form GET / HTTP/1.1
when the homepage (/
) is requested.
For example:
<If "%{THE_REQUEST} =~ m#^[A-Z]{3,7}\s/\s#">
#some other rules...
Header always set Test "It works"
</If>
Aside:
PassEnv REQUEST_URI
The PassEnv
directive is not required here. REQUEST_URI
is already an Apache server variable and available to your script.