RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
I understand everything in the line above except for the following segment:
([#?][^\ ]*)?\ HTTP/
I've done some research and found out that the square brackets are used to match any one of the characters within them. However, I have also learned that ?
is used to make the preceding token optional and that ^
means "match start."
As such, why do the square brackets in the segment above contain both ?
and ^
? I thought that the square brackets were simply used as a "character class."
Also, what is the purpose of HTTP/
in the segment specifically? All of my searches have come to no avail.
First understand what is THE_REQUEST
.
THE_REQUEST
variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is:
GET /index.php?id=123 HTTP/1.1
Now the part you want more clarification on:
([#?][^\ ]*)?\ HTTP/
Here is what is happening here:
?
or #
literally since inside [...]
almost all special characters are matched literally?
after ([#?][^\ ]*)
makes it an optional match\ HTTP
matches a space followed by HTTP
Now let me tell you that matching #
is not needed here since a web server never receives a URL after #
. That is all handled by client browsers.
It is better to use this RewriteCond
:
RewriteCond %{THE_REQUEST} ^(.+)\.php(\?\S*)?\ HTTP/ [NC]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With