Forum Moderators: phranque

Message Too Old, No Replies

Help with a Redirect with several variables

Apache redirect MOD_REWRITE .htaccess

         

edelen

9:49 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



For two weeks, I've been struggling with what should be a simple issue.

I'm not an expert on Apache server regex, so I need a pro who could help me route all the data from my old site to my new one. My old site has the URLs in this format:

http://yyy.com/YEAR/MONTH/FILENAME

OR

http://www.yyy.com/YEAR/MONTH/FILENAME

I want to redirect that to

http://zzz.com/YEAR/MONTH/

with the YEAR being 2003, 2004, 2005, or 2006, and the MONTH a number between 01 and 12.

I'm not going to attempt to match the filenames because the filenaming conventions are too significantly different, and I don't want to have to hand-modify several hundred filenames. The filename can just drop off in the rewritten URL. If I get people to the same month, that's good enough for me. I just don't want to give them a 404. All I need is to pass the YEAR and MONTH variables and Rewrite them to the new URL. I also want the rewritten URL to show in the browser.

Thank you in advance. If someone could help, I'd be very grateful!

g1smd

11:29 pm on Apr 7, 2007 (gmt 0)

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



It is disappointing that you didn't have a go at this first, because that is the only way to actually learn this stuff (that's mainly how I learnt this stuff). I'd start here, but I expect there are better ways...

RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^(.*)/(.*)/(.*)$
http://www.newdomain.com/$1/$2/ [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain.com\.com [NC]
RewriteRule ^(.*)/(.*)/(.*)$
http://www.newdomain.com/$1/$2/ [R=301,L]

RewriteCond %{HTTP_HOST} ^newdomain\.com [NC]
RewriteRule ^(.*)$
http://www.newdomain.com/$1 [R=301,L]

g1smd

11:53 pm on Apr 7, 2007 (gmt 0)

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



OK. There are various reasons why that would not work, so the next best guess is...

RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^/([0-9]{4}?)/([0-9]{2}?)/(.*)$
http://www.newdomain.com/$1/$2/ [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain.com\.com [NC]
RewriteRule ^/([0-9]{4}?)/([0-9]{2}?)/(.*)$
http://www.newdomain.com/$1/$2/ [R=301,L]

RewriteCond %{HTTP_HOST} ^newdomain\.com [NC]
RewriteRule ^(.*)$
http://www.newdomain.com/$1 [R=301,L]

It assumes that years are always four digits and months are always two digits (leading zero for 00 to 09).

edelen

11:54 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



g1smd,

Every time I've messed around with the .htaccess file I whacked something and took my site down. Do that enough times and you get wary of experimenting. Besides, I learn more by seeing how it's done right, then applying variations elsewhere.

Thank you very much for your help. I will try to write a regex onto the front of the one statement to check for "www".

g1smd

12:12 am on Apr 8, 2007 (gmt 0)

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



I maintain a local copy of Apache for experimenting with, with a fake domain name to access it, and with that domain name entered directly into the hosts file of my PC to make sure that requests for that fake domain name only go my local copy of Apache. Once things work on the local copy they then get re-edited and uploaded to the real site.

You know why that "500 Error" message occurs so many times when you are editing things? I think that it is because you need to see it at least 500 times before you really begin to understand this stuff.

edelen

12:58 am on Apr 8, 2007 (gmt 0)

10+ Year Member



g1smd,

I tried your suggested code and it didn't work. Got a 500 error.

edelen

1:34 am on Apr 8, 2007 (gmt 0)

10+ Year Member



A-ha!

I modified something else I once used (based on an idea G1smd mentioned) and now this works:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} yyy\.com
RewriteCond %{REQUEST_URI} ^/200(.*)/(.*)/(.*)
RewriteRule .* [zzz.com...] [R=301,L]

If anyone sees a fatal flaw in how that might return something other than what I want, please let me know!

HOWEVER...

The only problem that I see is with the first variable. I really want to limit the variable to 2003, 2004, 2005, or 2006. I will want to have a 2007 that DOES NOT redirect. How then would I limit the check in line #4 to those earlier years for redirect, but have no redirect for 2007?

edelen

2:15 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Okay, so I rewrote it this way to limit the year range:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} yyy\.com
RewriteCond %{REQUEST_URI} ^/(2003¦2004¦2005¦2006)/(.*)/(.*)
RewriteRule .* [zzz.com...] [R=301,L]

And that works nicely! Tested with a 2007 date and it didn't redirect--just what I wanted!

STILL...

Is there a more elegant way to write the year range? I can't find any regex for setting a range like that.

jdMorgan

3:00 am on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To fully-qualify the domains, years and months, I'd suggest:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?yyy\.com
RewriteRule ^(200[3-6])/(0[1-9]¦1[012])/[^/]*$ http://zzz.com/$1/$2/ [R=301,L]

Replace the broken pipe "¦" character in the RewriteRule pattern with a solid pipe character before use; Posting on this forum modifies the pipe character.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

edelen

6:56 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



jdMorgan,

Thanks for the more elegant solution. I implemented it and it works fine.

Blessings and thanks!