Question

I just find it hard and not sure how to use mod_rewrite for my domains and sub-domains.

all I want to do is to redirect the following:

1- from either www or non www domain to another domain

www.domain.net || domain.net --> mydomain.com

2- *force www sub-domain to non www sub-domain *

www.sub.domain.com --> sub.domain.com

I tried the following code for the 1st part and it worked, but got me in trouble causing some conflict with sub-domains.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.net$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

I believe there is a fix, but not sure how!!

I went through many articles, but didn't find the right solution.

any idea how to do that??

Thanks,

Was it helpful?

Solution

try ...

RewriteEngine On

RewriteCond %{HTTP_HOST} www\.domain\.net [NC]
RewriteRule ^(.*)$ http://mydomain.com [L,R=301]
RewriteCond %{HTTP_HOST} domain\.net [NC]
RewriteRule ^(.*)$ http://mydomain.com [L,R=301]
RewriteCond %{HTTP_HOST} www\.sub\.domain\.com [NC]
RewriteRule ^(.*)$ http://sub.domain.com [L,R=301]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top