htaccess 301 redirects with multiple domain RewriteCond and RewriteRule
-
30-05-2021 - |
문제
I'm trying to redirect all pages of an old domain to a page about that domain on a new domain.
I have built countless sites using the first 3 lines of the code below which redirects anything that is not www.mydomain.co.uk to www.mydomain.co.uk.
In the following 4 lines im trying to redirect all the pages of olddomain.co.uk to a single page on my new domain www.currentdomain.co.uk/page-about-olddomain
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.currentdomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} ^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/page-about-olddomain [R=301,L]
What i want to happen
What i expect to see from the above code is that the page www.olddomain.co.uk/test-page would redirect to www.currentdomain.co.uk/page-about-olddomain.
What actually happens
What i actualy see is the page www.olddomain.co.uk/test-page redirects to www.currentdomain.co.uk/test-page.
I believe this to be a conflict between the 2 sets of RewriteConds
Any help on this would me much appreciated and i'm sure there are other people wanting to do a similar thing.
Thanks
해결책 2
I fixed the issue by swapping the order of the rules thanks to @LazyOne and adding the [OR] condition after the first RewriteCond that checks for the olddomain with the www's
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/olddomain-page [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.currentdomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/$1 [R=301,L]
다른 팁
Order of rules matters. Swap them around -- move first rule (lines 1-3) below 2nd.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} ^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/page-about-olddomain [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.currentdomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.currentdomain.co.uk/$1 [R=301,L]