1

我已经搜索了这个问题很多天,并尝试了很多不同的方法,但到目前为止没有任何效果。我正在使用 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.comhttps://example.com. 但是,如果用户输入这样的地址:www.example.com/users他们将被重定向到https://example.com/index.php?qa-rewrite=users返回 404 错误的地址。

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

4

2 回答 2

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>
于 2014-12-30T07:06:24.293 回答
0

我用这个:

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]上面的

于 2021-09-19T15:56:47.770 回答