Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - old to new uri pattern matching

Need some help crafting mod_rewrite rules for new site version

         

lokust

5:23 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Hi there.
I need some guidance in creating a set of mod re_write rules to facilitate the move to an updated version of my website. The url structure has changed and since we have built up a decent search rank I'd like to seamlessly re-direct old to new urls. Here are some examples of the old to new url structures I'm dealing with:

-----------------
Type #1: Makes
-----------------

Sample 1:
old url: [abc.com...]
new url: [abc.com...]

Sample 2:
old url: [abc.com...]
new url: [abc.com...]

There are many makes so rather than set up a 301 statement for each make, it'd be best to pattern match.

So far:
RewriteRule ^([A-Za-z0-9-]+)[/]* used-$1-cars [R=301]

-----------------
Type #2: Models
-----------------

Sample 1:
old url: [abc.com...]
new url: [abc.com...]

Sample 2:
old url: [abc.com...]
new url: [abc.com...]

So far:
RewriteRule ^(used-[A-Za-z0-9]+-cars/[A-Za-z0-9-]+)/uk/results-page-0.html $1 [R=301,L]

-----------------
Type #3: Dealers
-----------------

Sample 1:
old url: [abc.com...]
new url: [abc.com...]

Sample 2:
old url: [abc.com...]
new url: [abc.com...]

Sample 3:
old url: [abc.com...]
new url: [abc.com...]

-----------------
Type #4: Cities
-----------------

Sample 1:
old url: [abc.com...]
new url: [abc.com...]

Sample 2:
old url: [abc.com...] keynes/ (note the space, ie: used-cars-stoke on trent)
new url: [abc.com...] (/cities/used-cars-stoke-on-trent)

Many thanks.

Al

g1smd

7:49 pm on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whenever you use the [R=301] flag, make sure the target of the redirect also mentions both the protocol and the domain name, as well as the using the [L] flag on every one.

jdMorgan

8:26 pm on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got several problems that are difficult or horrifyingly-inefficient to solve with mod_rewrite, due to the fact that mod_rewrite is not a full-featured scripting language by any stretch of the imagination.

If this were my site, I would internally rewrite all old URLs to a script, and do the external redirection from within that script. Things like case-conversion and swapping hyphens for spaces are simple in most scripting languages, but very painful and inefficient in mod_rewrite.

Jim

lokust

8:52 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Hi Jim. So you'd basically note down all old urls and the new url equivalents and 301 from within a php script?
I'm not sure if that's what you mean, though would it be equally viable to do this as individual 301 statements in .htaccess?

Thanks,

Al