views:

61

answers:

2

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.

+5  A: 

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.

outis
I can't use just QUERY_STRING for complicated reasons, but your answer does make sense. The following Still gives me 404: RewriteCond %{THE_REQUEST} subpage\?sid$RewriteRule ^.*$ /index.php [R=301,L,NC]
Antony Carthy
It's because "sid" isn't at the end of %{THE_REQUEST}; there's still the HTTP version. Why can't you use QUERY_STRING?
outis
A: 

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

Igor Zinov'yev