Mobile app version of vmapp.org
Login or Join
Carla537

: Apache htaccess - modify query string with mod_rewrite I've recently changed scripting on my website, and this has resulted in a few 404 errors as some very old queries are no longer being

@Carla537

Posted in: #Apache #Htaccess #ModRewrite

I've recently changed scripting on my website, and this has resulted in a few 404 errors as some very old queries are no longer being redirected correctly.

I've been trying to create a rewrite rule to fix this, as it requires modifying the query string.

This is an example of a broken URI:

/index.php?/page/index.html/_/news/this-is-a-page-r34


If I can rewrite the query string as so, the request will succeed:

/index.php?/article.html/_/news/this-is-a-page-r34


So, with this in mind I have tried various rewrite rules in my .htaccess file, but have not been having any success. Amongst a few other variations, I have tried the following:

1st attempt

RewriteCond %{QUERY_STRING} ^(.*)page/index.html(.*)$
RewriteRule /index.php /index.php?%1article.html%2 [L]


2nd attempt

RewriteCond %{QUERY_STRING} ^/page/index.html/(.*)$
RewriteRule /index.php /index.php?/article.html/ [L]


3rd attempt

RewriteCond %{QUERY_STRING} ^/page/index.html(.*)$
RewriteRule . /index.php?/article.html [L]


It does appear to be matching the RewriteCond, so that's something, but the output never seems to get rewritten according to the RewriteRule.

Can anybody see where I'm going wrong? I think I've got to the point where I've been looking at it for too long and have become code blind.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Carla537

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

You need to use %1 (not ) in your "2nd attempt" (as you did in your 1st attempt) and it looks like you should be almost there (see below for explanation):

RewriteCond %{QUERY_STRING} ^/page/index.html/(.*)$
RewriteRule ^index.php$ /index.php?/article.html/%1 [L]


If you're in .htaccess (or <Directory> section in server config) you need to also remove the slash prefix from the RewriteRule pattern. (This is part of the directory-prefix that is removed in per-directory rewrites.)

Literal dots should also be escaped in regex, otherwise they match any character.

%1 vs back-reference

%1 is a back-reference to the first parenthesised sub pattern (ie. captured group) in the last matched RewriteCond directive.

Whereas is a back-reference to the corresponding captured group in the RewriteRule pattern (ie. nothing in this case!).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme