【问题标题】:how to redirect non www url become www. into htaccess如何将非 www url 重定向为 www。进入 htaccess
【发布时间】:2020-08-05 06:25:41
【问题描述】:

我只想设置文件 .htaccess 以将非 www url 重定向到 www。但这种情况并不常见。

如果这样的网址https://<domain name>.com变成https://www.<domain name>.com

我已经设置了这样的代码。但没用

<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>

如果我们在浏览器中输入&lt;domain name&gt;.com,该代码就可以工作。但是当有人像这样在浏览器上手动输入https://&lt;domain name&gt;.com 我的意思是手动输入https:// 可以变成https://www.&lt;domain name&gt;.com

所以对于这个 .htaccess 我想做这样的结果

如果类型

&lt;domainName&gt;.com变成https://www.&lt;domainName&gt;.com

www.&lt;domainName&gt;.com 变成https://www.&lt;domainName&gt;.com

https://&lt;domainName&gt;.com变成https://www.&lt;domainName&gt;.com

http://&lt;domainName&gt;.com 变成https://www.&lt;domainName&gt;.com

请帮忙。

【问题讨论】:

    标签: php .htaccess url


    【解决方案1】:

    试试这个:

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

    它包括一个深层链接转发。

    【讨论】:

    • 感谢您的回复。但是对不起,它仍然没有重写url
    【解决方案2】:

    你可以试试这个

    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]
    

    【讨论】:

      【解决方案3】:

      您可以在站点根目录 .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]
      

      【讨论】:

      • 是的,它可以工作,但是当我只输入 .com 而没有 https:// 时。它不能添加www。在前面
      • 这不正确。由于OR 的第一个条件,此规则也会重定向http://example.com。确保这是最重要的规则,并在完全清除浏览器缓存后进行测试。
      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 2019-01-29
      • 2012-11-16
      • 2011-04-12
      相关资源
      最近更新 更多