Forum Moderators: phranque
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!
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]
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).
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".
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.
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?
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.
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]
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