Forum Moderators: phranque

Message Too Old, No Replies

Using a wild card for 301 L Redirections

         

omoutop

1:08 pm on Dec 30, 2005 (gmt 0)

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



Hi to all and HAPPY NEW YEAR!

I have a problem regarding htaccess and more specifically 301 L redirections..

I have a HUGE static web site which I now transform to dynamic and i dont want to loose my rankings :(

My first thought was to 'step' on the old urlz but after some analysis this proved to be impossible in some cases. So i will have to create hundreds of tiny rulz to redirect my old urls to the new ones! these rulz would look like:

######################################EXAMPLPE
RewriteRule ^crete/rethymno/hotels/rethymno-junior suite.htm$ [mywebsite...] suites-alpha.htm [R=301,L]
#####################################

I NEED MORE THAN 2000 RULZ to make it work...would it be possible to use a wildcard system?!

I hope i am clear enough to introduce you to my problem.

Hope the best and a HAPPY NEW YEAR

jdMorgan

1:25 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer depends on how the old URLs correspond to the new ones. For a typical dynamic site, just a few rules will suffice. I can't tell exactly what you're doing, though, because your new URL appears to be static as well.

The usual approach is to leave the URLs alone, and simply do something like this:


RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^.]+)\.html$ /script.php?country=$1&city=$2/lodging=$3&provider=$4 [L]

This simply parses your static URL and converts it to name/value pairs which are then used to call the script that generates dynamic pages. The client (browser or robot) is not redirected, and so sees no change whatsoever in your URLs. Links published on the 'new' dynamic pages continue to use static URLs. Basically, the outside world sees no changes whatsoever, and the only change is the method your server uses to serve pages; it now uses 'script.php' to create them, instead of serving static content.

You will probably need to add a few execptions to the above rule to allow your error pages to be static, since it's a good idea to keep error pages very simple and independent of any scripting support... In other words, keep your error pages static, so that they will still work even if your script fails.

Jim

omoutop

1:55 pm on Dec 30, 2005 (gmt 0)

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



thx jdMorgan for ur reply!

I need to give you some more hints....

you mentioned "I can't tell exactly what you're doing, though, because your new URL appears to be static as well. "

This is because my new urlz have already been converted from php to htm (just a few lines before my 301 L redirections).....in plain words....
I first convert my php pages to htm
#########################################
RewriteRule ^(.*)/(.*)/hotels/(.*)-(.*)-category-(.*).htm$ list_category.php?area=$1&island_name=$2&island_name=$3&tname=$4&cat_name=$5 [nc]
########################################since the .htm created is not identical to my old page (static one_in most cases canot be done=EXCEPTION) I need another rule send all rankings to my new .htm......All these EXCEPTIONS are about 2000 so i need to use a wild card for the second phase of redirections.....
something like:
########################################

RewriteRule ^(.*)/(.*)/hotels/(.*)-(.*).htm$ [mysite...] [R=301,L]
########################################

jdMorgan

2:38 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well then, try it. It should work fine, with a few minor exceptions that you'll have to code on a case-by-case basis. Just make sure the RewriteRule pattern can distinguish between an old URL and a new one so that you don't create an 'infinite' redirection loop.

One warning though: Avoid the use of multiple (.*) patterns in your rules. They are easy to write and easy to understand, but they are *horribly* inefficient -- subpattern processing time increases proportionally with the length of the 'tail' string (the remainder of the string following the current subpattern) and overall processing time increases geometrically with the number of (.*) subpatterns. See the example I posted above for a much better pattern. The regular-expressions tutorial cited in our froum charter may be helpful to decode it.

Jim

omoutop

3:04 pm on Dec 30, 2005 (gmt 0)

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



thx again jdMorgan!

I will try it out on monday and report back, I ve heard again for the (.*) patterns and from now on I will use ([^/]+) toavoid server load!

jdMorgan

3:19 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should use [^x]+ where the 'x' is the delimiter character that follows the part you want to match. [^x]+ means, "one or more characters not equal to 'x'". Therefore, the regex parser 'knows' immediately when it should stop matching for the current subpattern, rather than matching *all* characters in the string, and then having to 'back off' repeatedly to get a match.

Notice that I used [^/]+ for all slash-delimited groups except the last, where I used [^.]+ because the delimiter for the filetype is "."

Jim