Forum Moderators: phranque

Message Too Old, No Replies

Redirect and RewriteRule

         

jrosell

12:00 pm on Feb 27, 2012 (gmt 0)

10+ Year Member



I previosly have a list with all pages like this:
Redirect 301 /oldpage1.html http://example.com/newpage1.html
Redirect 301 /oldpage1.html http://example.com/newpage1.html

Now I tried to move from old domain to newdomain in RewriteRule and Redirect 301 stop working.

I tried to use both %{REQUEST_URI} and %{HTTP_HOST} this way without success:


RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^/oldpage2.html /newpage2.html [R=301,L]
RewriteRule ^/oldpage2.html /newpage2.html [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/%{REQUEST_URI} [R=301,L]

I didn't find useful examples or clear help on that.
Any help there?

[edited by: jrosell at 12:20 pm (utc) on Feb 27, 2012]

g1smd

12:10 pm on Feb 27, 2012 (gmt 0)

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



The RegEx pattern in a RewriteRule should not begin with a leading slash when the rule is in the htaccess file.

The redirect targets should include the protocol and hostname.

The REQUEST_URI line is not needed at all.

Escape literal periods in RegEx patterns.

Use example.com in this forum to suppress URL auto-linking.

Add a blank line after every RewriteRule and a # comment before each block of code explaining what that code does.

The HTTP_HOST pattern should be
!^(www\.example\.com)?$
where www.example.com is the new site. This then takes control of all non-canonical hostnames, even those where the request includes a port number.

jrosell

12:42 pm on Feb 27, 2012 (gmt 0)

10+ Year Member



In this case, olddomain and newdomain are in diferent servers.

Thanks, I can get that working:

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.cat [NC]
# Redirect pages from olddomain

RewriteRule o1.html http://www.example.com/n1.html [R=301,L]
RewriteRule o2.html http://www.example.com/n2.html [R=301,L]
# List of pages of olddomain to redirect to newdomain

RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
# When a page is not listed, redirect to new domain home page

[edited by: jrosell at 1:03 pm (utc) on Feb 27, 2012]

g1smd

12:56 pm on Feb 27, 2012 (gmt 0)

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



The RewriteCond pattern should change to that I suggested above and be re-attached to the rule it belongs with. It will then redirect all requests that are not for "exactly" www.example.com and so will work perfectly well on the old or the new domain.

Escape literal periods in RegEx patterns.

Add a blank line after every RewriteRule so you can clearly see which bits of code are separate items.