简体   繁体   English

如何重定向非 www url 成为 www。 进入 htaccess

[英]how to redirect non www url become www. into htaccess

i just want to setting file .htaccess for redirect non www url to www.我只想设置文件 .htaccess 以将非 www url 重定向到 www。 but the case is not common.但这种情况并不常见。

if the url like this https://<domain name>.com become https://www.<domain name>.com 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.如果我们在浏览器中仅键入<domain name>.com ,该代码就可以工作。 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 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所以对于这个 .htaccess 我想做这样的结果

if type如果类型

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

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

https://<domainName>.com it become https://www.<domainName>.com https://<domainName>.com它变成https://www.<domainName>.com

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

please help.请帮忙。

You may use this rule in site root .htaccess:您可以在站点根目录 .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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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