homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

redirect help needed

 7:28 am on May 12, 2004 (gmt 0)


I recently used mod rewrite to make one of the dynamic urls appear static


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


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


Now my question is is it possible to do a permanent redirect of the page [mydomain.com...]
[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.



 1:27 pm on May 12, 2004 (gmt 0)


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.

Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions tutorial [etext.lib.virginia.edu]



 2:55 pm on May 12, 2004 (gmt 0)

Hi 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.


 7:07 pm on May 12, 2004 (gmt 0)


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]

With the elimination of the redundant (and unneeded) directives, addition of start anchors, use of negative character matching instead of the "promiscuous" ".*" (which requires mod_rewrite to "back into" each match), and the removal of character escaping in the substitution (again, not needed), this should run faster. Also, the substitution for a redirect should include the canonical URL as shown.

If this code is for use in httpd.conf rather than .htaccess, then you'll need to start each pattern with "^/" instead of just "^".



 1:58 am on May 13, 2004 (gmt 0)

Have incorporated the chages as you suggested. Thanks.

Its really good to have someone experienced look at your code.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved