Forum Moderators: phranque

Message Too Old, No Replies

Please help me 301 redirect olddomain/olddir

olddomain/oldirectory/ works, but olddomain/olddirectory doesn't

         

Clark

11:33 pm on Sep 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a 301 Redirect in my .htaccess that works fine for:

[olddomain...] but it doesn't redirect [olddomain...] and I am baffled. What's the correct code for this.

Note: The redirect is for another domain altogether, NOT a new file in the same domain.

jdMorgan

1:42 am on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without seeing your redirect code, it's a bit difficult to answer...

Jim

Clark

2:16 am on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah. I was looking for the correct way to do it rather than my way :) Actually I tried it many different ways that didn't work :) Here's the latest that's in the file and after double checking it only redirects this way:

[olddomain.com...]

but if I go to this url:

[olddomain.com...]

then it doesn't redirect. So here's my code, which resides in
[olddomain.com...]
:

RewriteEngine On
RewriteRule /directory/index.php [newdomain.com...] [R=301,NC,L] #note that this line was just a prayer that didn't work :)#
RewriteRule /directory [newdomain.com...] [R=301,NC,L]
ErrorDocument 404 [newdomain.com...]

I had also tried this in the past and it didn't work:

RewriteRule /directory/ [newdomain.com...] [R=301,NC,L]

jdMorgan

3:09 pm on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two problems:

1) The URL-paths tested by the RewriteRule pattern in .htaccess are stripped of their leading slash, because .htaccess operates in a per-directory context. Therefore, the directory-level part of the URL - the slash - is removed. And therefore, your pattern, in which you specify a leading slash, would never match.

2) If you use a canonical URL for ErrorDocument, the result will be a 200-OK server response (see the notes in the DirectoryIndex documentation). Better to return a 404 response, and then use your catch-all 301 redirect to pass the client's request for the error document on olddomain over to newdomain *after* the 404 response.


ErrorDocument 404 /directory/index.php
#
RewriteEngine on
RewriteRule ^directory http://www.newdomain.com/index.php [R=301,NC,L]

Note: I removed the specific rule to redirect index.php, because it should not be necessary. If you find you need to restore it, then correct it per note 1 above.

Just in case: Don't put comments after your directives, put them on a new line. Comments appended to directives can cause Apache Warnings, which will slow down your server, even if the warnings don't appear in the error log because your loglevel is set higher than "Warn".

Jim

Clark

6:05 pm on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. I'll try it. In the actual file I have no comments at all btw, but it's good to know.