Htaccess rewrite for subdomain

Trying to make a mod_rewrite for

a) [I]http://subdomain.domain.com/folder/filename.html[/I]

to a new different domain at

b) [I]http://www.newdomain.com/samefolder/samefilename.html[/I]

So that url’s of the old domain are redirected to exactly the same page in the new domain

The following rewrite does not work as this ads the subdomain from a) to the newdomain at b):

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

This rewrite leads to

[I]http://www.newdomain.com/oldsubdomain/samefolder/samefilename.html[/I]

where I want it to redirect to

[I]http://www.newdomain.com/samefolder/samefilename.html[/I]

Q:
Is there a way around using regular expressions, maybe by skipping the subdomain in the $1 backreference?
The restriction is that it should apply only to this specific subdomain from the old domain. Other subdomains may not be affected…

Regards,
Enno

Enno,

When you used [U]http://subdomain.domain.com/folder/filename.html[/U], did that remain in the location window or did you see http://www.domain.com/subdomain/folder/filename.html?

I think that your host’s setup is probably of the type which does NOT allow the subdomain to remain in the URL (some cPanels do that, some don’t).

To get around that, you’ll have to capture the subdomain in a RewriteCond statement and include it in the regex of the associated RewriteRule, i.e., something like:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z]+)\\.domain\\.com$ [NC]
RewriteRule ^%1/(.*)$ http://www.newdomain.com/$1 [R=301,L]

Regards,

DK

DK thanx for bothering…

To answer your first question:

[I]http://subdomain.domain.com/folder/filename.html[/I] remained in the location bar and is allowed by the host.

We just decided to move the files for this subdomain to a new domain and decided not to use a subdomain as a url anymore.

Is seems that your solution is directed towards all sudomains in the old domain. Since we are running a CMS with its own .htaccess directives, this does not apply.

I have tried your code code in original and adjusted form even with the subdomain alone like in:

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

But I get 403 forbidden and 500 internal server errors (for the error pages) due to - I assume - <Filesmatch> and ErrorDocument directives in the .htaccess of the CMS that is running on the same server.

It’s a litte bit complicated situation since the subdomain I want to redirect resides on the same server as the CMS but is not a part of the CMS. We used the old subdomain for CNAME forwarding to a different site.

It seems that only non-specific redirects to the new domain work, like:

RewriteCond %{HTTP_HOST} ^subdomain\\.domain\\.com$ [NC]
RewriteRule ^/.*$ htpp://www.newdomain.com/ [L,R=301]

Or is there still another workaround for specific url’s in this case ?

Regards,
Enno

Enno,

Okay, that’s a bit of help!

The reason that my code didn’t work after you removed the regex for the subdomain is that there was NO %1 value for the RewriteRule!

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

I don’t quite understand how your server would provide the domain’s address for the subdomain access (unless your CMS’s mod_rewrite is interfering) but the above code should make that irrelevant.

Regards,

DK

DK,

your RewriteRule did it!

Thanx for your help on this one!

Regards,
Enno