【问题标题】:Apache2 .htaccess redirect to https not workingApache2 .htaccess 重定向到 https 不起作用
【发布时间】:2016-06-14 02:00:09
【问题描述】:

我意识到这个问题得到了很多回答,但由于某种原因其他解决方案对我不起作用。我正在尝试将 http 重写为 https,但是当我尝试使用 http 浏览我的网站时仍然收到“错误请求”。

这是我的 .htaccess 中的代码:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我在 VirtualHost 配置中有 AllowOverride All 并且我确保 mod_rewrite 已启用。

感谢任何帮助。如果您需要更多信息,请告诉我。

编辑:

这是我完整的 .htaccess 文件:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

可能的解决方案: 我使用的是 Apache 2.2。这可能是旧版本 Apache 的错误。我要更新了,等我知道了再发帖。

【问题讨论】:

    标签: apache .htaccess ubuntu mod-rewrite https


    【解决方案1】:

    首先,您应该确保在您的 apache 上启用 mod_rewritessl,通过此代码

    sudo a2enmod rewrite
    sudo a2enmod ssl
    

    然后重启你的apache

     sudo service apache2 restart
    

    现在按照以下步骤操作

    sudo nano /etc/apache2/sites-enabled/000-default.conf 
    

    并添加此代码

    <VirtualHost *:80>
        ServerName my.site.com
        ServerAlias my.site.com
        DocumentRoot /var/www/SITE-PATH
        <Directory /var/www/SITE-PATH>
            AllowOverride All
            Order Allow,Deny
            Allow from All
        </Directory>
        ErrorLog /var/log/apache2/project_error.log
        CustomLog /var/log/apache2/project_access.log combined
    </VirtualHost>
    

    然后转到您的网站目录/var/www/SITE-PATH 并将此代码添加到您的.htaccess

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

    为了您在路径 /etc/apache2/sites-enabled/default-ssl.conf 上的 ssl 信用 你应该有这样的东西

    <IfModule mod_ssl.c>
        <VirtualHost _default_:443>
            ServerAdmin webmaster@localhost
            ServerAdmin admin@example.com
            ServerName my.site.com #important
            ServerAlias my.site.com #important
            DocumentRoot /var/www/SITE-PATH #important
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            SSLEngine on
            SSLCertificateFile /etc/apache2/ssl/apache.crt #important
            SSLCertificateKeyFile /etc/apache2/ssl/apache.key #important
            SSLCertificateFile  /etc/ssl/certs/ssl-cert-snakeoil.pem
            SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
            <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
            </FilesMatch>
            <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
            </Directory>
            BrowserMatch "MSIE [2-6]" \
                    nokeepalive ssl-unclean-shutdown \
                    downgrade-1.0 force-response-1.0
            BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
        </VirtualHost>
    </IfModule>
    # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
    

    如果你走的所有步骤都正确,你应该有这个

    http://my.site.com => 重定向到 => https://my.site.com

    编辑: 有关 SSL 的更多详细信息,请关注 How To Create a SSL Certificate on Apache for Ubuntu 14.04this post

    【讨论】:

    • 感谢您的回复。不过,我仍然收到“400 Bad Request”。
    • @CoryPuckett 你的系统是什么?你的意思是当你通过https打开网站时,你得到400。或者重定向到https你得到400??
    • 我正在运行 Ubuntu 14.04。如果我使用“https://my.site.com”打开我的网站,它工作正常。只有当我尝试“http://my.site.com”时,我才会收到“400 Bad Request”
    • @CoryPuckett 你能添加你所有的 htaccess 吗?
    • 我已将我的 .htaccess 添加到原始帖子中。
    【解决方案2】:

    尝试: 重写引擎开启 RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    【讨论】:

    • 感谢您的建议。我刚刚尝试过,但仍然收到“错误请求”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2017-06-21
    • 2018-09-08
    • 2016-04-12
    • 1970-01-01
    • 2016-05-24
    • 2023-03-03
    相关资源
    最近更新 更多