【问题标题】:htaccess Redirect All Traffic to httpshtaccess 将所有流量重定向到 https
【发布时间】:2014-08-23 17:26:42
【问题描述】:

在使用 htaccess 做事时,我是一个初学者,所以请耐心等待我这个愚蠢的问题。我知道这已经解决了很多(例如hereherehere),但它似乎不适用于我的情况。我的浏览器在我的 htaccess 文件中显示带有此代码的“重定向循环”错误:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Trying to redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

在我重定向到 https 之前,我知道这可能与 RewriteCondRewriteRule 有关,但我真的不知道我在这里做什么,也不知道要更改什么.

更新:

更多可能有用的信息:

  1. 当我删除“重定向到 https”代码并手动输入 https://my.site.com 时,它会正常加载。
  2. 此外,在我不小心删除了目录中的 .htaccess 文件之前,重定向到 https 的功能非常有效。
  3. 我尝试重定向的应用程序位于另一个应用程序的子文件夹中,该应用程序也有一个 .htaccess 文件。这是该应用程序的代码:

    <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteEngine On </IfModule>

萤火虫是这样说的:

【问题讨论】:

    标签: .htaccess redirect https


    【解决方案1】:

    更改您的规则的顺序,并在其他内部重写规则之前保留 301 规则:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Trying to redirect to https
        RewriteCond %{HTTPS} off
        RewriteCond %{SERVER_PORT} !=443
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
    </IfModule>
    

    【讨论】:

    • 仍然得到“此网页有重定向循环”
    • 在不同的浏览器中测试。
    • 刚刚在 Firefox 中测试并收到类似的消息:“Firefox 检测到服务器正在以永远不会完成的方式重定向此地址的请求。”
    • 如果我删除 [L,NE,R=301] 行,它会加载正常,但它不会重定向到 https:并且我的样式、图像、js 都不会加载。
    • 我正在使用 laravel(这是一个 php 框架,而不是 CMS)。您认为这可能是我的托管服务提供商的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2013-01-06
    • 2014-04-27
    • 1970-01-01
    • 2011-03-15
    • 2016-09-26
    • 2016-07-27
    • 2020-05-16
    相关资源
    最近更新 更多