Forum Moderators: phranque
I recently used mod rewrite to make one of the dynamic urls appear static
[mydomain.com...]
This is what I used
RewriteEngine on
RewriteBase /
RewriteRule new-page-(.*)-(.*)-(.*)\.php$ /index\.php?var1=$1&var2=$2&var=$3
After some time I realised that var2 was redundant and thus changed the rewrite rule to
[mydomain.com...]
RewriteEngine on
RewriteBase /
RewriteRule new-page-(.*)-(.*)\.php$ /index\.php?var1=$1&var3=$2
Now I realise that yahoo has already picked up the previous version which is [mydomain.com...]
This version still works with the new rewrite rule with some detrimental effects on the page.
The mod rewrite rule is now having the following effect
var1="value1-value2"
var3=value3
Now my question is is it possible to do a permanent redirect of the page [mydomain.com...]
to
[mydomain.com...] Or is there an alternative solution. (I have got some decent indexing for the first version.)
I hope I have been able to explain the problem. Any help will be highly appreciated.
Thank you.
PS. I do not have much experience in mod_rewrite or 301 redirect, all this stuff is what I have picked up through google.
Welcome to WebmasterWorld [webmasterworld.com]!
Yes, certainly, you can fix the problem. The trick is to handle the three-variable URL version first, redirecting it to the appropriate two-variable URL. This is then followed by a rule that will handle the two-variable version if the first rule does not find three variables.
The $1, $2, and $3 variables in the substitution (the stuff on the right side) of the rule back-reference (contain the matched value of) the parenthesized sub-pattterns in the pattern (the stuff on the left side) of the rule, in order, 1 through 9.
So, in your first rule, match all three variables, but leave out $2 on the right side. This has the effect of requiring it to be there in the requested URL, but "throwing it away" once it it found.
If the request is for three variables, this first rule should remove the redundant one and do an external (301 permanent) redirect, and no more rewriting will be needed for this HTTP request; The client will issue a new request using the two-variable URL you provided.
If the request contains only two variables, the pattern in this first rule will not match, so the rule won't be invoked. Therefore, you need to provide a second rule to handle the two-variable requests. This second rule should be an internal rewrite only (like the code you posted), and not a redirect.
In accordance with our charter [webmasterworld.com], we encourage you to learn about mod_rewrite, and to post your "best effort" code here if you have problems and need help.
Ref:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions tutorial [etext.lib.virginia.edu]
Jim
thanks for the detailed explanation
As you said that the three variable version should be handled first and redirected to the two variable url leaving out variable two and doing a permanent redirect
RewriteEngine on
RewriteBase /
RewriteRule new-page-(.*)-(.*)-(.*)\.php$ /new-page-$1-$3\.php [L,R=301]
This I have followed up with the two variable mod_rewrite version as earlier
RewriteEngine on
RewriteBase /
RewriteRule new-page-(.*)-(.*)\.php$ /index\.php?var1=$1&var3=$2
Tested it...and it works :)
Thanks again.
Now, let's speed it up:
RewriteEngine on
RewriteBase /
RewriteRule ^new-page-([^-]*)-([^-]*)-([^.]*)\.php$ http://www.yourdomain.com/new-page-$1-$3.php [R=301,L]
RewriteRule ^new-page-([^-]*)-([^.]*)\.php$ /index.php?var1=$1&var3=$2 [L]
If this code is for use in httpd.conf rather than .htaccess, then you'll need to start each pattern with "^/" instead of just "^".
Jim