【问题标题】:Impossible to force www URL in HTTPS无法在 HTTPS 中强制使用 www URL
【发布时间】:2018-03-18 11:55:38
【问题描述】:

我正在尝试将我网站中的所有传入请求重定向到:https://www.example.com

我能够重定向 HTTP 流量,无论有无 www,但我无法让它与非 www HTTPS 请求一起工作。我的意思是:

  • http://example.com -> 正确重定向
  • http://www.example.com -> 正确重定向
  • https://example.com -> 不重定向

我在我的.htaccess 文件中尝试了许多规则,但它们似乎都不起作用。

我在 CentOS 机器上使用 Apache/2.2.15。我还使用 mod_jk 模块将所有流量重定向到 Tomcat。

我的配置文件如下:

mod_jk.conf

LoadModule jk_module "/etc/httpd/modules/mod_jk.so"
JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile     /var/run/httpd/mod_jk.shm
JkLogFile     /var/log/httpd/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

workers.properties

workers.apache_log=/var/log/httpd
worker.list=app1Worker
worker.app1Worker.type=ajp13
worker.app1Worker.host=localhost
worker.app1Worker.port=8009

app1.conf

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    CustomLog /var/log/httpd/app1_access.log combined
    ErrorLog /var/log/httpd/app1_error.log

    #<IfModule mod_jk.c>
       JkMount /* app1Worker
    #</IfModule>

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

listening.conf

Listen 217.61.129.109:80
Listen 217.61.129.109:443

.htaccess

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=301]

我在.htaccess 文件中尝试了许多其他规则,但都没有奏效。

你能说出最后一个案例不起作用的原因吗?

UPDATE-1

很抱歉造成误解,但我只包含括号,因为 StackOverflow 会抱怨其他问题。只是忽略他们。 (现已删除)

AllowOverride的配置在Apache的主httpd.conf文件中:

DocumentRoot "/var/www/html"
<Directory />
    Options SymLinksIfOwnerMatch
    AllowOverride All
</Directory>

<Directory "/var/www/html">
    Options Indexes SymLinksIfOwnerMatch
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

我将AllowOverride None 更改为AllowOverride All,因为一开始就是这个原因,.htaccess 文件根本没有被处理。

现在我已经测试了这个文件正在被处理,因为如果我评论它的所有内容,那么 HTTP 重定向就不起作用。我的问题仅在于非 www HTTPS 重定向。

我的感觉是.htaccess 中的规则是正确的,但肯定还有一些我遗漏的东西。

UPDATE-2 已解决!!!

往下看答案

【问题讨论】:

  • 你在哪里启用 .htaccess 在你的 https 配置中? (即AllowOverride 指令)。由于协议周围的括号会可怕地破坏这个重定向,我怀疑它根本没有被执行。但是,由于您可以访问服务器配置,因此您应该在那里执行此重定向,而不是 .htaccess。然后你的 80 端口的 VirtualHost 将有一个简单的Redirect。仅为您的规范主机和Redirect 创建一个单独的 VirtualHost。并且一如既往地清除浏览器缓存。
  • 我已经更新了我的帖子,请检查。我试图在 VirtualHost 上进行重定向,它可以将 http 流量从端口 80 重定向到 https。但是我怎样才能强制那里有 www 的 https 网址?
  • 您应该添加您的“UPDATE-2”作为答案并接受它(我会支持它!:) 顺便说一句,如果您格式化为,括号不是必需的代码(内联或块) - 我已编辑删除它们(它们具有误导性)。
  • 完成了。感谢您的帮助。

标签: apache .htaccess redirect mod-rewrite https


【解决方案1】:

已解决!!!

好吧,我现在不知道为什么 .htaccess 过滤器不适用于提到的特殊情况,但根据 @MrWhite 的建议,我让它与 &lt;VirtualHost&gt; 配置一起使用。

我像这样更新了我的 listener.conf 文件:

NameVirtualHost *:80
Listen 80
NameVirtualHost *:443
Listen 443

我的 app1.conf 是这样的:

<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
   ServerName example.com
   Redirect permanent / https://www.example.com/
   SSLEngine on
   SSLCertificateFile    /etc/letsencrypt/live/example.com/cert.pem
   SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
   SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    CustomLog /var/log/httpd/app1_access.log combined
    ErrorLog /var/log/httpd/app1_error.log
    #Redirect all traffic to Tomcat
    JkMount /* app1Worker
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

感谢您的帮助。

【讨论】:

  • 非常感谢您回来并发布您的解决方案。这有很大帮助。 :) +1
猜你喜欢
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2013-05-31
  • 1970-01-01
  • 2018-05-04
  • 2012-01-20
相关资源
最近更新 更多