Why am I getting a redirect loop with Wordpress htaccess file?

时间:2015-11-12 11:39:23

标签: wordpress apache .htaccess redirect

I'm trying to do a site-wide redirect from http to https for my Wordpress, and I'm trying to follow this tutorial - https://blog.nraboy.com/2014/12/force-https-pages-wordpress-site/

This seems to work for them, but when I follow it, I get a message saying the website is performing in a way that will never complete (or redirect).

My htaccess file currently looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Can anyone please help me identify what's causing the redirect loop and how to fix it? I've put it into various testers, which seem to run through it fine. I have no plugins running, and can't figure out what's causing the loop.

1 个答案:

答案 0 :(得分:0)

我通常会将自定义重写规则添加到默认WP上面。

<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite non www. to www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Rewrite HTTP 2 HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# BEGIN WordPress
...

正如您所看到的,这两个规则都重定向到HTTPS(第一个规则仅在非www。时应用)。

每当请求以https://www开头时。标准的WP规则(已发布,以及保存在.htaccess文件中的下面)将按需使用。