htaccess重定向不会转到https

时间:2018-08-16 16:54:00

标签: wordpress apache .htaccess redirect

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

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

Redirect 301 /career.html /careers/

在我的任何其他客户端站点上,将用户从https://example.com/career.html转移到https://example.com/careers/都是一跳。在此客户的网站上,用户首先进入http://example.com/careers/(无论初始请求中是否有https),然后再次被用户转到https://example.com/careers/

如果未通过特定的“重定向301”规则重定向URL,则一次可以正确重写URL。我需要此重定向才能一跳重定向到https。它可以在我处理的所有其他站点上运行,但不能在此站点上运行。

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

下面是htaccess的一部分,用于规范化为https://www。我必须编写非常具体的重定向规则以在该服务器上规范化为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,然后对于https获得301,最后是具有正确https URL的200OK。如果“重定向301”规则将https版本排除在外,所有这些都可以避免。知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

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