【问题标题】:Redirecting HTTP to HTTPS correctly in Question2Answer script在 Question2Answer 脚本中正确将 HTTP 重定向到 HTTPS
【发布时间】:2014-12-30 07:02:32
【问题描述】:

我已经搜索这个问题很多天并尝试了很多不同的方法,但到目前为止没有任何效果。我正在使用 Question2Answer 脚本,我想将所有 HTTP 请求重定向到 HTTPS。

我的 URL 结构设置为:

/123/why-do-birds-sing (requires htaccess file)

htaccess文件如下:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>

这会将http://example.com 正确重定向到https://example.com。但是,如果用户输入这样的地址:www.example.com/users,他们将被重定向到https://example.com/index.php?qa-rewrite=users,这会返回 404 错误。

index.php?qa-rewrite= 是自动添加的,从 htaccess 中删除它会完全搞砸一切,我认为它应该在那里。

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect question2answer


    【解决方案1】:

    这是因为您需要所有重定向规则任何路由规则(没有R 标志的那些),所以:

    DirectoryIndex index.php
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
    RewriteRule . %1/%2 [R=301,L]
    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
    </IfModule>
    

    【讨论】:

      【解决方案2】:

      我用这个:

      DirectoryIndex index.php
      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /
      
          RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
          RewriteRule . %1/%2 [R=301,L]
      
          RewriteCond %{HTTPS} off
          RewriteCond %{HTTP:X-Forwarded-SSL} !on
          RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
          RewriteCond %{HTTP_HOST} ^www\.example\.com$
          RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
      
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
      </IfModule>
      

      如果您不喜欢重定向www,可以删除该行 RewriteCond %{HTTP_HOST} ^www\.example\.com$ 和上面的[OR]

      【讨论】:

        猜你喜欢
        • 2014-02-20
        • 2020-02-09
        • 2022-01-06
        • 2019-10-21
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-29
        相关资源
        最近更新 更多