Apache .htaccess重写不起作用

大家一直把我的头撞在这个墙上,看了很多文章。

我的任务

我试图重写一个wordpress网站的3个部分的HTTPS,如果他们通过HTTP访问:

/cart/ /my-account/ /checkout/ 

以及这些重写的worpress已经添加了一个重写删除index.php的URL。

index.php重写是唯一的工作。

我的configuration

这里是我的.htaccess

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / #BEGIN MyRewrite RewriteCond %{HTTPS} !on RewriteCond %{REQUEST_URI} (checkout|cart|my-account) RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L] #END MyRewrite # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress </IfModule> 

问题

似乎我确实被redirect到https,但然后循环,从不显示。

一些curltesting

在下面你可以看到我的访问http版本/购物车被正确地告知,这已经转移到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> 

重写规则按顺序处理,前两条规则( index.php规则)会捕获所有可能的请求。 由于它们都有[L]修饰符,因此规则处理在它们匹配时会停止,它们总是这样做。

index.php规则下的redirect规则从来没有被testing过。 尝试将它们移动到文件的顶部(但是 RewriteEngine on在线之后)并且在底部具有index.php catchall规则。


作为一个方面说明,我强烈build议使用curl而不是使用浏览器来testingredirect。 这个避免的两个具体问题是cachingredirect和链接redirect(这是URL1redirect到URL2,redirect到URL3的地方,在浏览器中看不到URL2)。

curl -I <URL>curl --include <URL>将完成这项工作。

编辑:下面是错误的 – 这将是有效的,如果这是在VirtualHost上下文,但因为它在.htaccess中,它不是。

现在我有更多的咖啡了,我看到你需要在RewriteRule中包含第一个/ 。 喜欢这个:

 RewriteRule ^/secure https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]