【问题标题】:Apache2 sites-enabled/deafault.conf redirects not proper?Apache2 sites-enabled/deafault.conf 重定向不正确?
【发布时间】:2020-02-03 09:39:27
【问题描述】:

我的 apache2 配置有以下问题: 我在etc/apache2/sites-enabled-文件夹中有这个文件:

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

<VirtualHost _default_:443>
  ServerName example.com
  DocumentRoot /var/www
  SSLEngine On
 # etc...
</VirtualHost>

如果我在浏览器中输入https://example.com/mysite/start.php,一切都很好。 但是如果我输入 http://example.com/mysite/start.php,我会被重定向到 https://example.com/mysite/start.phpmysite/start.php,因此会收到 404 错误消息。 我需要在配置文件中进行哪些更改以防止出现该问题?感谢您的帮助

编辑: 如果我输入http://example.com/mysite/,我会成功重定向到https://example.com/mysite/start.php

【问题讨论】:

    标签: debian apache2 http-redirect apache-config


    【解决方案1】:

    您应该使用重写规则,而不是使用永久重定向来实现 https。 将您的代码替换为以下内容:

    <VirtualHost *:80>
       ServerName example.com
       DocumentRoot /var/www
       RewriteEngine On
       RewriteCond %{HTTPS} off
       RewriteRule (.*) https://%{SERVER_NAME}/ [R,L]
    </VirtualHost>
    
    <VirtualHost _default_:443>
      ServerName example.com
      DocumentRoot /var/www
      SSLEngine On
     # etc...
    </VirtualHost>
    
    

    我们在这里使用重写规则来激活您的网站 SSL。

       RewriteEngine On
       RewriteCond %{HTTPS} off
       RewriteRule (.*) https://%{SERVER_NAME}/ [R,L]
    

    并确保您在 apache 中启用了 Rewrite 模块。 要启用 modrewrite,请访问 apache2 模块目录 cd /etc/apache2/mods-available 运行以下命令 a2enmod rewrite.load 并重新启动 apache2 service apache2 restart

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多