【问题标题】:Force https on apache在 apache 上强制 https
【发布时间】:2019-07-09 23:51:33
【问题描述】:

我使用 Apache 服务器发布我的网站。问题是我想强制我的网站使用 HTTPS。例如,如果有人输入 URL“http://www.alireza.co/contact.html”,它应该重定向到“https://alireza.co/contact.html”。

我使用以下规则创建了一个.htaccess 文件:

# To use 404.html as default 404 error page.
ErrorDocument 404 /404.html


# To force website use https and remove www. subdomain.
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://alireza.co/$1 [R=301,L]

现在的问题是每当我输入 URL 时,无论是使用 httpshttp,还是使用 www. 或没有它,我都会在 Firefox 中收到错误 too many redirectsthe website is not redirecting properly

我使用Autistici.org 作为我的主机提供商。我的网站是静态的(仅限 HTML/CSS)。我尝试了很多方法,但都没有成功。

【问题讨论】:

    标签: apache .htaccess ssl https no-www


    【解决方案1】:

    我刚刚想通了。我唯一要做的就是用这些简单的规则重写我的.htaccess 文件:

    # To remove www. from URL
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.alireza\.co [NC]
    RewriteRule ^(.*)$ https://alireza.co/$1 [L,R=301]
    #To force HTTPS
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    # To make 404.html default 404 error page
    ErrorDocument 404 /404.html
    

    删除www..htaccess 规则很简单,但我让它重定向到https://alireza.co/$1 而不是http://alireza.co/$1。

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 2016-09-03
      • 2014-05-25
      • 2015-06-25
      • 2021-08-20
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 2011-09-07
      相关资源
      最近更新 更多