I have this in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
What I am trying to do is add another RewriteRule to this configuration so that old URLs like these will be redirected to the root of the site
http://www.acme.com/category.php?id=6
http://www.acme.com/product.php?id=183&category=
I know that “category.php” and “product.php” will now be a invalid strings. Using “category.php” as an example I tried altering the config to be this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category.php(.*) / [L,R=301]
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
This will redirect as follows
http://www.acme.com/category.php?id=112 -> http://www.acme.com/?id=112
I have two problems
- I don’t want the query string to be appended. I haven’t specified
[QSA] - If I enter the root directly
www.acme.comthe page won’t display
unless I remove the line I just added. Why would my new RewriteRule
affect URL’s that dont start with “category.php”?
[Edit]
I have also tried this instead of the redirect above, but I still seem to be getting both rules being applied
RewriteRule ^category.php /holdingPage.php [L]
You need the 2 conditions to be tied to the old rule that sends everything through
index.phpand you need to add your new rule above them:To deal with 1., I’ve added a “?” at the end of the / in the rule’s target. That makes it so query strings won’t automatically get appended unless you specifically have the
QSAflag. As for 2., not sure why it wasn’t working for you, but it may have had something to do with the 2 conditions that were being applied to the wrong rule.EDIT:
For a description of the 2
RewriteCondlines, see this post: https://stackoverflow.com/a/11275339/851273A
RewriteCondis essentially a conditional, this block:is essentially this (in pseudo code):
The thing is, a
RewriteCond, or many of them one directly after another, only applies to the immediately followingRewriteRule, so without moving the category.php rule out in front, the pseudoc deo equivalent would have been:Which is kind of broken because the 2
if()conditionals are being mis-applied. Specifically the one that rewrites to “index.php” sort of requires the conditionals in order to prevent itself from looping.