简体   繁体   中英

Redirect non WWW to WWW using htaccess without failing subdomains

I need to redirect non WWW to WWW without failing sub domains. Please help me to do this.

mydomain.com > www.mydomain.com
m.mydomain.com > m.mydomain.com
mydomain.com/page.php > www.mydomain.com/page.php

I am using URL rewrite. they should also remain working. This is the current htaccess file.

Options All -Indexes
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]
RewriteRule ^vid vid.php [NC,L] 
AddType application/octet-stream mp3 
RewriteCond %{HTTPS} =on 

Thank you.

This rule should work for non-www to www for main domain only:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM