MOD_REWRITE循环,在WordPress博客的特定部分上将http重定向到https

时间:2013-02-19 作者:squareborg

我正在尝试将wordpress站点的3个部分重写为https,如果它们是通过http访问的:

/cart/

/my-account/

/checkout/
除了这些重写之外,worpress还添加了一个重写来删除索引。php退出url。索引。php重写是唯一有效的方法。这是我的。htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|cart|my-account)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]

# BEGIN WordPress
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

# END WordPress
在下面的一些Curl测试中,您可以看到我访问http版本的/cart,正确地告诉我它已移动到https/cart,因此我尝试使用https版本,告诉它已移动到相同的https版本,从而形成一个循环。

PS C:\\Users\\Stephen> C:\\Users\\Stephen\\Downloads\\curl-7.23.1-win64-ssl-sspi\\curl.exe -k -i http://www.mysite.com/cart
HTTP/1.1 302 Found
Date: Wed, 20 Feb 2013 09:07:06 GMT
Server: Apache
Location: https://www.mysite.com/cart
Vary: Accept-Encoding
Content-Length: 285
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://www.mysite.com/cart">here</a>.</p>
<hr>
<address>Apache Server at www.mysite.com Port 80</address>
</body></html>

PS C:\\Users\\Stephen> C:\\Users\\Stephen\\Downloads\\curl-7.23.1-win64-ssl-sspi\\curl.exe -k -i https://www.mysite.com/cart
HTTP/1.1 302 Found
Date: Wed, 20 Feb 2013 09:07:06 GMT
Server: Apache
Location: https://www.mysite.com/cart
Vary: Accept-Encoding
Content-Length: 285
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://www.mysite.com/cart">here</a>.</p>
<hr>
<address>Apache Server at www.mysite.com Port 80</address>
</body></html>
它重定向到https,但随后陷入循环,浏览器会抱怨出现循环。这里有重写大师。

1 个回复
SO网友:Josh Mountain

我决不是一个mod\\u重写专家,但我会喜欢这项工作吗?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Force HTTPS for /cart/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\\s/cart [NC]
RewriteRule ^(cart) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Force HTTPS for /my-account/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\\s/my-account [NC]
RewriteRule ^(my-account) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Force HTTPS for /checkout/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\\s/checkout [NC]
RewriteRule ^(checkout) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# BEGIN WordPress
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

# END WordPress
我相信这三个独立的陈述可以以某种方式结合起来,但正如我所说,我不是专家。让我知道这是否适合您。

结束

相关推荐

WordPress在更新时会更改.htaccess文件吗?

我刚刚实现了一个新的WP网站,取代了一个旧的硬编码HTML网站。我将在中添加301个重定向.htaccess 文件将旧URI指向新的WP永久链接。我的问题是,由于WP生成.htaccess 文件最初(至少在我的安装中是这样)将WP覆盖.htaccess 我以后更新WP时的文件?除此之外,是否有任何时候,包括上述更新,WP可能会覆盖此文件,如果有,是否有办法防止?我到处寻找,似乎找不到一个直截了当的答案。任何帮助都将不胜感激。谢谢