【问题标题】:http://example.com would not redirect to https://www.example.comhttp://example.com 不会重定向到 https://www.example.com
【发布时间】:2017-12-21 18:36:11
【问题描述】:

我们的网络服务器上有这两个配置文件。

http:

<VirtualHost *:80>
    ServerName www.example.de
    ServerAlias example.de www.example.at example.at www.example.net example.net

    RewriteEngine On
    RewriteCond %{HTTP_HOST} off
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}
   # Error page is just the index telling about the situation of not being connected
    ErrorDocument 404 /index.html
</VirtualHost>

https:

<IfModule mod_ssl.c>
NameVirtualHost *:443
<VirtualHost *:443>
    ServerName www.example.de
    ServerAlias example.de www.example.at example.at www.example.net example.net

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/www.example.de/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.de/privkey.pem

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}$1 [R=301,L]

    DocumentRoot /var/www/homepage/web
    <Directory /var/www/homepage/web>
            AllowOverride All
            Order Allow,Deny
            Allow from All
    </Directory>



            ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
LogLevel warn

    ErrorLog ${APACHE_LOG_DIR}/example-ssl.error.log
    CustomLog ${APACHE_LOG_DIR}/example-ssl.access.log combined

</VirtualHost>
</IfModule>

当我尝试打开http://www.example.com 时,它会被重定向到https://www.example.com

当我尝试打开 http://example.com 或 https://example.com 时,出现错误“页面不可用”

我在网络服务器上的两个 conf 文件有什么问题? 两者都应该重定向到https://www.example.com url。

【问题讨论】:

  • 不,HTTP_HOST 不会包含值 off ...除非您尝试访问 http://off,否则不会包含值
  • 我应该在哪里添加这个 HTTP_HOST,我可以将其设置为关闭?
  • 为什么不配置没有重定向的 apache 虚拟主机并从 .htaccess 文件进行重定向?
  • 我想,在 Apache conf 中会更容易
  • “我应该在哪里添加这个 HTTP_HOST,我可以将其设置为关闭?” - 无处,因为这甚至没有意义。您在这里访问了错误的变量。你想检查HTTPS 变量。

标签: apache redirect


【解决方案1】:

请尽量避免使用 mod_rewrite,除非没有其他选择。如您所见,它很复杂,并且会让您的生活更难完成更简单的任务。

一个重定向就可以将所有内容重定向到 SSL 虚拟主机:

Redirect / https://www.example.de/

注意:重定向依赖于 mod_alias,因此在非 SSL 虚拟主机中不需要 RewriteEngine 等。


如果你坚持使用 mod_rewrite 因为你有很多名字并且需要变量:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R,L]
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]

第一个捕获主机不以 www 开头的情况,第二个捕获其余情况。

【讨论】:

猜你喜欢
  • 2020-12-21
  • 2020-09-24
  • 1970-01-01
  • 2016-01-16
  • 2015-09-25
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 2014-10-02
相关资源
最近更新 更多