【问题标题】:Stop WWW for sub-domain with htaccess使用 htaccess 停止子域的 WWW
【发布时间】:2022-01-07 20:14:51
【问题描述】:

我的subdomain.example.com 重定向到www.subdomain.example.com

Htaccess 代码:

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### END WWW & HTTPS

我在找

example.comwww.example.com

anything.example.comanything.example.com

我该如何解决这个问题?

更新:我正在为子域和根域使用一个 htaccess 文件,但出于某种原因,我不能为不同的域使用单独的 htaccess 文件。

注意:请不要将此问题标记为重复。我已经搜索了 stackoverflow 的其他部分,但没有找到可行的解决方案。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    您需要限制您的重写条件以仅匹配顶级域:

    # ensure www. to top level domain only
    RewriteCond %{HTTP_HOST} ^[\w-]+\.(com|net|in|co\.uk)$ [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ### END WWW & HTTPS
    

    确保在测试前清除浏览器缓存。

    【讨论】:

    • 代码中不提及域名怎么办。我有多个别名不同的域指向同一个文件夹,如果我在 htaccess 代码中输入一个域,它将不适用于其他域。
    • 您想要为其添加 www 的顶级域的示例有哪些?
    • example.com example.net domain.co.uk ,别名域可以是不同的名称和TLD
    • 是否会被这种模式覆盖:^[\w-]+\.(com|net|in|co\.uk)$ ?
    • 是否可以不使用域或域扩展名?就像你在这里提到的:stackoverflow.com/questions/35285074/…
    【解决方案2】:

    解决方案:https://stackoverflow.com/a/35285128/4643961

    ### START WWW & HTTPS
    
    # ensure www.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # remove www if subdomain
    RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ### END WWW & HTTPS
    

    替代方案:

    ### START WWW & HTTPS
    
    # add www if not subdomain
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # remove www if subdomain
    RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ### END WWW & HTTPS
    

    【讨论】:

      【解决方案3】:

      供将来参考:

      # ensure www. to top level domain only
      RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
      RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      
      # ensure https
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteCond %{HTTPS} off
      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      
      ### END WWW & HTTPS
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 2012-12-06
        • 2016-01-21
        相关资源
        最近更新 更多