我正在尝试将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,但随后陷入循环,浏览器会抱怨出现循环。这里有重写大师。
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
我相信这三个独立的陈述可以以某种方式结合起来,但正如我所说,我不是专家。让我知道这是否适合您。