Forum Moderators: phranque
I've tried looking for a thread covering this but to no avail and the links from the charter make for some heavy reading and I've not been able to understand them (they're obviously not written for idiots ;)).
Will the below code do it?
RewriteEngine on
RewriteCond %{HTTP_HOST} .
# to redirect abc.com/ and www.abc.com/ ---> www.newdomain.com/abc/
RewriteCond %{HTTP_HOST}!^www\.abc\.com
RewriteRule ^(.*)$ [newdomain.com...] [r=301,L]
#to redirect abc.com/dir/ and www.abc.com/dir/ ---> www.newdomain.com/abc/dir/
RewriteCond %{HTTP_HOST}!^www\.abc\.com\dir
RewriteRule ^(.*)$ [newdomain.com...] [r=301,L]
#to redirect abc.com/dir/page.htm and www.abc.com/dir/page.htm ---> www.newdomain.com/abc/dir/page.htm
RewriteCond %{HTTP_HOST}!^www\.abc\.com\dir\page.htm
RewriteRule ^(.*)$ [newdomain.com...] [r=301,L]
The "newdomain" already has a 301 in place for all non-www to www.
RewriteRule ^abc/dir/(.*)$ http://www.newdomain.com/abc/dir/$1 [R=301,L]
RewriteRule ^abc/(.*)$ http://www.newdomain.com/abc/$1 [R=301,L]
The first will redirect abd/dir/anything to the same path/location on the new domain.
The second rule picks up anything in abc/ that is not in dir.
Remember to put the most specific rules at the top of the file, and the ones with the 'catch-alls' at the end.
Hope this helps.
Justin
BTW if the new site has the same directory/page structure, just use this:
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
As per your advice the first rewrite rules should be for the pages. Would the lines read like this?
RewriteRule ^abc/dir/page.htm(.*)$ [newdomain.com...] [R=301,L]
And, in your RewriteRule ^abc/(.*)$ in your second rule is there no need to mention the .com TLD?
<added> It doesn't have the same directory structure. Four of those pages are getting redirected to four different pages on four different domains.
RewriteRule ^abc/(.*)$ in your second rule is there no need to mention the .com TLD?
Only on the right side of the rule...
And the order you have is correct:
# Specific Pages - no need for the 'catch-all (.*) if you have the full path
RewriteRule ^abc/dir/page1\.htm$ http://thedomainofyourchoice/whatever/you/want1.html [R=301,L]
RewriteRule ^abc/dir/page2\.htm$ http://thedomainofyourchoice/whatever/you/want2.html [R=301,L]
RewriteRule ^abc/dir/page3\.htm$ http://thedomainofyourchoice/whatever/you/want3.html [R=301,L]
RewriteRule ^abc/dir/page4\.htm$ http://thedomainofyourchoice/whatever/you/want4.html [R=301,L]
# Everything Else
RewriteRule (.*) http://thedomainofyourchoice/$1 [R=301,L]
Justin
For testing purposes I've even shortened it to just this:
RewriteEngine on
RewriteRule ^abc/dir/(.*)$ [newdomain.com...] [R=301,L]
RewriteRule ^abc/(.*)$ [newdomain.com...] [R=301,L]
I went further and just used this:
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
But I still get the same errors. :(
I've managed to get .htaccess working. Apparently there was a problem that the hosting company had to sort out. I had to prove to them that the problem was at their end as they kept blaming my code and said they couldn't advise on htaccess. I proved the fault was at their end by using the exact same non-www to www code that's working on another account I've got with them but which is not working on this account. They finally admitted to the issue and resolved it. I then retried all of the above and most of it worked ... but one niggling matter remained:
RewriteRule ^abc/dir/(.*)$ [newdomain.com...] [R=301,L]
is not redirecting to www.newdomain.com/abc/dir but to www.newdomain.com/dir/ (which doesn't exist). I've tried everything and in the end just worked around it by creating a www.newdomain.com/dir/ and redirecting that to www.newdomain.com/abc/dir.
Had to do that for the individual pages as well unfortunately. They were going to www.newdomain.com/page1.htm instead of www.newdomain.com/abc/page1.htm. But, the solution + workaround has got everything redirecting to exactly where I wanted it. Thanks once again.