首页 > 解决方案 > htaccess 重定向不会转到 https

问题描述

这是我第一次在这里问任何问题,但多年来我一直在使用这个网站作为参考。非常感谢大家多年来的贡献。他们真的很有帮助!我现在遇到一个问题,我找不到确切的答案,这真的让我很沮丧。如果这里已经有答案,请原谅我,但我无法让搜索返回任何有用的信息。

我有一个 htaccess 文件,它没有将单个重定向发送到 URL 的 https 版本。以下规则的行为与此客户在其他网站上的行为不同。

Redirect 301 /career.html /careers/

在我的任何其他客户端站点上,将用户从https://example.com/career.html带到https://example.com/careers/一次。在此客户端的站点上,它首先将用户带到http://example.com/careers/(不管初始请求是否有 https),然后再次 301 到https://example.com/careers/

如果 URL 没有被特定的“重定向 301”规则重定向,则 URL 会被一次性正确重写。我需要此重定向以一跳重定向到 https。它适用于我处理的所有其他网站,但不适用于这个网站。

这是一个 wordpress 站点,wp-config 列出了 URL 的 https 版本。

下面是处理规范化为https://www的 htaccess 部分。我必须编写非常具体的重定向规则来规范化到此服务器上的 https,因为即使它们在 htaccess 测试仪和其他实时服务器上工作,其他任何东西都不起作用。这台服务器的某些地方并不像我预期的那样运行。

# Begin completely over-complicated but necessary redirects for canonicalization. Nothing else works on this dumb server.
#
# If https is off and there is no trailing slash, rewrite to https and /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off and there is a trailing slash, just write to https.
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} (/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If https is on and there is no trailing slash, rewrite to add /
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off, there is no trailing slash, and no www, rewrite to https www and /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off and there is no www but there is a trailing slash, rewrite to https www
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} (/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If https is on but no www and no trailing slash, rewrite to add www and trailing slash
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
#
# End of canonicalization redirects.

如果我请求重定向 URL 的 http 版本,则 URL 首先被重写为 https,然后它进入非 https 的 301,然后它获得 301 的 https,最后是正确的 https URL 的 200OK。如果“重定向 301”规则会退出 https 版本,那么所有这些都可以避免。知道这里发生了什么吗?

标签: wordpressapache.htaccessredirect

解决方案


If https:// is not in the prefix, the HTTP link loads instead. Add the following into your .htaccess in between the <IfModule mod_rewrite.c> tag:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]


If there were no additional modifications done to your .htaccess, it should look like the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
# END WordPress


推荐阅读