Asim Zaidi
Asim Zaidi

Reputation: 28284

what is this htaccess rule actually doing

I have this condition and rule and want to know what actually is going on here

RewriteCond %{REQUEST_URI} ^/sitepages/newversion/
RewriteCond %{QUERY_STRING} ^(.*[?|&])page=dynamicpage
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]

Also very interested in a detail explaination of this line

RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]

thanks

Upvotes: 0

Views: 26

Answers (2)

3bh
3bh

Reputation: 836

RewriteCond %{REQUEST_URI} ^/sitepages/newversion/

If the request URL starts with "/sitepages/newversion/",

RewriteCond %{QUERY_STRING} ^(.*[?|&])page=dynamicpage

and the query string contains "page=dynamicpage",

RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]

then take the part of the URL after "sitepages/newversion/" and redirect the request to "/pages/oldversion/(the rest of the url)".

For example:

http://domain.com/sitepages/newversion/someleaf?page=dynamicpage

will get redirected to

http://domain.com/pages/oldversion/someleaf?page=dynamicpage

Upvotes: 1

anubhava
anubhava

Reputation: 784918

First of all your rule can be rewritten better as this:

RewriteCond %{QUERY_STRING} (?:^|&)page=dynamicpage(?:&|$) [NC]
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L,NC]

Now for explanation part.

  • RewriteCond is matching a URI with query parameter page=dynamicpage
  • RewriteRule is matching a URI with pattern sitepages/newversion/(.*)$ which will match /sitepages/newversion/abc123 or /sitepages/newversion/foobar
  • (.*) is capturing group to populate $1 with the value abc123 in my first example.
  • In target we use /pages/oldversion/$1 that will become /pages/oldversion/abc123 for same example.
  • NC flag is for no case comparison.
  • L flag is for Last that makes mod_rewrite run the rewrite loop again.

Reference: Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

Upvotes: 1

Related Questions