Non www.htaccess redirect - ignore other subdomains

I have a .htaccess redirect for "non www":

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

it works. But I also have some subdomains besides www. If I call, for example, http://shop.example.com , it redirects me to: http://www.shop.example.com

I do not want to write subdomains to the .htaccess file, it should work automatically, just ignore anything else besides www, and: 'as follows:

 if subdomain =='' -> redirect to www.HTTP_HOST.... elseif subdomain !='' && subdomain !='www' -> do nothing. 

thanks!

+7
redirect apache .htaccess mod-rewrite
source share
3 answers

try the following:

 Options +FollowSymlinks RewriteEngine On RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC] RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301] 

Just tried it with internetagentur.drupal-webmaster.de (subdomain) - the main thing is without www.

+1
source share

Try the following rule:

 RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

Also consider HTTPS:

 RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
+10
source share

If you want a php solution, you can use something similar to this:

 define('URL', 'yourdomain.com/'); // fix : impose www rule if (strpos($_SERVER['SERVER_NAME'], 'www') === false ) { header('Location: http://www.'.URL.$_SERVER['REQUEST_URI']); die(); } 

This will also be redirected to the originally requested page.

0
source share

All Articles