【问题标题】:htaccess Redirect Wildcard Subdomains to https (Including www)htaccess 将通配符子域重定向到 https(包括 www)
【发布时间】:2014-11-12 00:05:10
【问题描述】:

目前,我的 htaccess 文件将我所有子域的非 https 请求重定向到 https...,除了 www 请求。 IE。 http://subdomain.example.com 被重定向到 https://subdomain.example.com(这是正确的),但 www.subdomain.example.com 被重定向到 https://www.subdomain.example.com(不正确)。这是我的 .htaccess 代码:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Trying to redirect to https
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} !=443
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

【问题讨论】:

  • 阅读评论# Trying to redirect to https下的三行如果HTTPS如果关闭并且我们不在端口443上提供服务,则重定向到主机(http://和.com之后的尾随/之间的所有内容) .您实际上是使用他们为主机放置的任何内容将所有内容重定向到 HTTPS。
  • 没错,但这并不能回答我的问题。我将如何修改此代码以将 www 也重定向到 https?我是 htaccess 方面的初学者。

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

尝试改变这个:

RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+\.example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

【讨论】:

  • 随着这一变化,subdomain.example.com 被重定向到 https://www.subdomain.example.comwww.subdomain.example.com 仍然被重定向到`subdomain.example.com
  • @sterfry68 如您所见,it works perfectly fine for me
  • @sterfry68 你清除浏览器缓存了吗?这些规则运行良好。
猜你喜欢
  • 1970-01-01
  • 2019-01-23
  • 2018-08-10
  • 1970-01-01
  • 2018-06-02
  • 2012-01-21
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多