Mobile app version of vmapp.org
Login or Join
Nimeshi995

: Redirect with HTTP_HOST redirects to sub subdomain The settings I have the following .htaccess rule RewriteCond %{HTTP_HOST} !^www. RewriteCond %{HTTP_HOST} !^de. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/

@Nimeshi995

Posted in: #Htaccess #Redirects #Subdomain

The settings

I have the following .htaccess rule

RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} !^de.
RewriteRule ^(.*)$ www.%{HTTP_HOST}/ [R=301,L]


This works great to redirect any subdomain that is not valid (www or de) to the www subdomain.

I have seen solution, but they only depend on single domain sites.

I have multiple domains:
www.example.com http://www.example.net de.example.com

which all serve their own brand of site.

Now I have the .htaccess rule that is mentioned above and it works beautifully if someone types in example.com it redirects to www.example.com.
The problem

If someone types in test.example.com, it redirects to www.test.example.com and if someone types in test.example.net, it redirects to www.test.example.net.
The question

How can I make it so that it redirects test.example.com to www.example.com? And test.example.net to www.example.net.
I need an domain insensitve solution because more domains will be added in the future.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Nimeshi995

1 Comments

Sorted by latest first Latest Oldest Best

 

@Cugini213

To redirect using the sub-domain as a parameter:


# HTTP 301 Redirect test.*.net -> *.net RewriteCond %{HTTP_HOST} ^test.([^.]+).net$ [NC]
RewriteRule (.*) www.%1.net/ [R=301,L)

# HTTP 301 Redirect test.*.com -> *.com RewriteCond %{HTTP_HOST} ^test.([^.]+).com$ [NC]
RewriteRule (.*) www.%1.com/ [R=301,L)


If you also want to redirect when there's no subdomain (e.g. example.com) to the main website using the subdomain (http://www.example.com):


# HTTP 301 Redirect *.net -> *.net RewriteCond %{HTTP_HOST} ^([^.]+).net$ [NC]
RewriteRule (.*) www.%1.net/ [R=301,L)

# HTTP 301 Redirect *.net -> *.net RewriteCond %{HTTP_HOST} ^([^.]+).com$ [NC]
RewriteRule (.*) www.%1.com/ [R=301,L)


If you're trying to achieve a lot with your .htaccess files you may find it helps to test your conditions and how they affect URL rewriting on the htaccess Made-With-Love website.

In case this is where you struggled, it is always worth remembering that in .htaccess, parameters prefixed with percent symbols (e.g. %1) reference values from the last matched RewriteCond expression, normally values that have been marked as being of interest by being surrounded in brackets (), and parameters prefixed with dollar symbols (e.g. ) reference values in the RewriteRule expression, normally values that are automatically assigned by Apache and relating to the URL.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme