2015-07-03 93 views
2

我的.htaccess中的代码将下面的代码重定向到https,除了一个(/ wc-api/v2),它不能使用SSL。问题.htaccess将所有页面重定向到HTTPS除了一个

它确实将所有页面重定向到https,但是如果我转到/ wc-api/v2,它会将我重定向到/index.php。

# Custom Redirect 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{HTTPS} !^on$ 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteRule (.*) https://example.com/$1 [R,L] 
</IfModule> 
# End Custom Redirects 

# 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] 
</IfModule> 
# END WordPress 

仅供参考第二块重定向是Wordpress所必需的。我也无法将它们合并成一个单独的块,或者Wordpress会覆盖我的更改。

回答

4

.htaccess上下文中配置重写时,在应用上次规则后,整个过程重新开始,使用内部重写的URL作为新的“输入”,直到没有更多规则匹配。

这意味着,当你请求/wc-api/v2,它没有改写为HTTPS,因为它没有你的第一个RewriteCond,因此,在接下来的块规则将其重写为/index.php内部。

现在“下一轮”开始,以/index.php作为输入。 RewriteCond %{REQUEST_URI} !^/wc-api/v2现在确实导致“真”,因为REQUEST_URI不再是/wc-api/v2,现在是/index.php。为此,该RewriteRule应用 - 你将被重定向到https://example.com/index.php

为了避免这种情况,你必须添加其他RewriteCond,这将防止应用的REQUEST_URI /index.php以及规则:

RewriteCond %{HTTPS} !^on$ 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteRule (.*) https://example.com/$1 [R,L] 
0

我得到一个错误我的建议服务器上的上方,从而修改了它稍微,发现这个工作对我来说:

RewriteEngine On 

RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteRule (.*) https://www.example.com/$1 [R,L] 


# 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] 
</IfModule> 

# END WordPress 

希望它为你工作。

相关问题