Forum Moderators: phranque

Message Too Old, No Replies

Redirecting "SubDomain/folder" to a new "subdomain.domain"

Help with .htaccess in getting 301 permanent redirects

         

AjAxed

7:59 am on Mar 23, 2006 (gmt 0)

10+ Year Member



Hi, this is my problem:

I earlier used a [subdomain.olddomain.com...] as my website and [subdomain.olddomain.com...] for my blog address.

Now recently I've moved to [newdomain.com...] which is entirely for me. I've set up the blog at [subdomain.newdomain.com...]

Now the issue is, while I can redirect from [subdomain.olddomain.com...] to [newdomain.com...] which works and forwards the trailing uri's properly, when I try to forward [subdomain.olddomain.com...] to the [subdomain.newdomain.com...] it actually goes to
[subdomain.newdomain.com...]

For the parent folder which maps to [subdomain.olddomain.com,...] the .htaccess reads:


RewriteEngine on

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^newDomain\.com
RewriteRule (.*) http://www.newdomain.com/$1 [R=301]

and for the blog folder which maps to [subdomain.olddomain.com...] the .htaccess reads:


RewriteEngine On
RewriteCond %{HTTP_HOST}!^subdomain\.olddomain\.com [NC]
RewriteRule .* http://subdomain.olddomain.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST}!^subdomain\.newdomain\.com
RewriteRule (.*)$ http://subdomain.newdomain.com/$1 [R=301,L]

But this is not working correctly.. If I type [subdomain.olddomain.com...] (Note the trailing slash) it works fine and keeps the trailing URI intact and hence goes to [subdomain.newdomain.com...]
But if I enter [subdomain.olddomain.com...] (No trailing slash) it goes to [subdomain.newdomains.com...] where the part in quotes is the folder path from the old server..

Can someone help me clean this up?

Thanks a lot.

jdMorgan

5:42 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be due to the configured canonical server name, and UseCanonicalName being set to 'on'. Or it could be a problem with misconfigured DocumentRoot and Alias directives.

Apache sees the missing trailing slash, invokes mod_dir, and then uses the ServerName to build the new URL (see the Apache docs for details).

If you don't have httpd.conf access, the simplest solution would be to add a rule to detect missing trailing slashes, so that this does not happen. for example:


# Add trailing slash if missing (skip rewrite for paths with a
# period in the last part, already ending in slash, or blank)
RewriteCond %{REQUEST_URI} !(\.[^/]+¦/)$
RewriteRule (.+) /$1/

Place this code above your other code, and don't use an [L] flag.

Replace the broken pipe "¦" character above with a solid pipe before use; Posting on this baord modifies the pipe character.

Jim

AjAxed

6:11 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



Thanks for the quick reply :)
Just for clarification, should I put your code in the subdomain htaccess or the subfolder htaccess?

And yeah, it's a shared hosting box so I don't have httpd.conf access :(

jdMorgan

1:11 am on Mar 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not familiar with the details of your site, so I don't know. Try it in either or both places -- it won't do anything if it's not needed -- and then test.

Jim

AjAxed

10:49 am on Mar 24, 2006 (gmt 0)

10+ Year Member



I tried it but unfortunately, it does not seem to help..
I mean, the trailing / does does come but it is still appended with the path of the directory from the old server!

Do you need more info that the explanation above?

Thanks for your time :)

jdMorgan

1:14 am on Mar 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is rather odd, and I'd suggest you contact your host for help.

Jim