简体   繁体   中英

how to redirect non www url become www. into htaccess

i just want to setting file .htaccess for redirect non www url to www. but the case is not common.

if the url like this https://<domain name>.com become https://www.<domain name>.com

i already set the code like this. but didn't work

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

that code is working if we type just <domain name>.com in browser. but when someone type manualy on browser like this https://<domain name>.com i mean type manualy the https:// can it turn into https://www.<domain name>.com

so for this .htaccess i want to do result like this

if type

<domainName>.com it become https://www.<domainName>.com

www.<domainName>.com it become https://www.<domainName>.com

https://<domainName>.com it become https://www.<domainName>.com

http://<domainName>.com it become https://www.<domainName>.com

please help.

You may use this rule in site root .htaccess:

RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Try this:

# uniform host name
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com$0 [R=301,L]

It includes a deep link forwarding.

You can try this

RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [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