Forum Moderators: phranque

Message Too Old, No Replies

Some htaccess help please

I've tried finding the solution in previous threads

         

oddsod

6:43 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Problem: I'm closing down one site and need to redirect its 15 pages/directories to 15 different locations. But I want to redirect both the www and non-www versions of each page/directory.

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.

jd01

7:02 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just remove the conditions and specify the directory youwould ike to redirect and you should be fine:

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]

oddsod

7:11 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your reply Justin :)

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.

jd01

7:47 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

oddsod

7:56 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow, great! I think it's all clear now. I'll give it a shot.

I really appreciate your help, Justin. What would the Apache Web Server forum do without you and jdmorgan?!

oddsod

1:14 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Help! I've done something wrong. It's showing a 403 for abc.com and it's showing a 404 for abc.com/dir/ :(

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. :(

jdMorgan

3:08 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Have you flushed your browser cache between tests?

2) Are these domains hosted on the same "account" on one server? Different accounts? Different servers?

Jim

oddsod

8:55 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The abc domain that I'm discontinuing is added to one of my accounts at Westhost (it's not the main account - it uses a /var/www/html/abcdomain directory). The site I'm redirecting to is a different site on a different host/server. I have flushed my cache. Have checked that the DNS is pointed correctly by temporarily taking off the htaccess in /var/www/html/abcdomain and putting an index.htm file in there. Then when I type abcdomain.com in a browser I get to see this index.htm file.

jdMorgan

4:27 am on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This sure is odd. What is in your server error log?

Have you tried using a Redirect [httpd.apache.org] ( mod_alias [httpd.apache.org] ) directive instead of mod_rewrite?

Jim

[added] Linked to mod_alias docmentation for reference. [/added]

[edited by: jdMorgan at 10:19 pm (utc) on Sep. 2, 2005]

oddsod

8:27 am on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I could try that Jim. Do I just replace the words "rewrite" with "redirect"?

oddsod

3:21 pm on Sep 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some feedback guys in case it helps anybody else browsing this thread.

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.