Mod Rewrite complete befuzzlement [closed]
-
05-07-2019 - |
Vra
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule subpage\?sid /index.php [R=301,L,NC]
My request is http://example.com/subpage?sid
It keeps returning 404 Not Found, rather than redirecting to index.php
I have tried not escaping the ? and changing the request to http://example.com/subpage\?sid
I have tried loads of things to try get this working but am now stumped.
Oplossing
The QUERY_STRING is not part of the URI. From the documentation for RewriteRule:
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the
%{QUERY_STRING}
variable.
As the doc says, you'll need to use a RewriteCond, something like:
RewriteCond %{QUERY_STRING} (^|&)sid([&=]|$) [NC]
RewriteRule ^/?subpage /index.php [R=301,L,NC]
If %{QUERY_STRING} truly isn't an option, try:
# Apache is supposed to use PCRE, but doesn't seem to like "\\s"
RewriteCond %{THE_REQUEST} subpage?sid[^!-~] [NC]
RewriteRule ^/?subpage /index.php [R=301,L,NC]
Also, you almost never need RewriteBase as you're using it ("RewriteBase /"). Comment the line and test it to be sure, but you'll probably find you can delete it with no ill affect.
Ander wenke
Maybe you need to check the VirtualHost setting for your site and set the AllowOverride setting for the corresponding directory?
<Directory /var/www/vhosts/some_host>
....
AllowOverride All
</Directory>
Here's more about apache's AllowOverride