Could someone explain this RewriteCond to me? Especially the HTTP/ portion at the end:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.XXXXXXXXXXXXXXXX.com/ [R=301,L]
I found a very good explanation for the first half of the condition already here, but I'm still confused with the "/index.php\ HTTP/" portion.
And again: Why the "HTTP/" ?
CodePudding user response:
I found a very good explanation for the first half of the condition already here, but I'm still confused with the "/index.php\ HTTP/" portion.
That explanation is omitting some detail. THE_REQUEST
server variable contains the first line of the HTTP request headers. And will contain a string of the form:
GET /index.php HTTP/1.1
So, it contains 3 pieces of information, separated with spaces. You have the request method (GET, POST, OPTIONS, HEAD, etc.), the URI and the protocol/version used (HTTP v1.1 in this case).
For this particular regex you don't necessarily need to include the final HTTP/
part, just up to and including the space. However, by including http/
it makes the regex easier to read and less prone to error. It would not be best practice to end on a space (that you can't see).
The following would be equivalent:
RewriteCond %{THE_REQUEST} \s/index\.php\s
This uses the shorthand character class for whitespace (\s
) instead of escaping a literal space (so you can "see" the space).
Or, use double quotes to surround the argument:
RewriteCond %{THE_REQUEST} " /index\.php "
(Although, that almost looks like an error at first glance.)
The reason for including this condition (RewriteCond
directive) in the first place is to prevent a rewrite loop should you be rewriting the request to index.php
later (a front-controller pattern). THE_REQUEST
is not updated throughout the request (unlike other variables) when it is internally rewritten by Apache.