简体   繁体   English

.htaccess的问题,除了一个页面之外,所有页面都重定向到HTTPS

[英]Issue with .htaccess redirecting all pages to HTTPS except one

I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL. 我在.htaccess中有以下代码,可将所有页面重定向到https以外的页面,其中一个(/ wc-api / v2)不能使用SSL。

It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php. 它确实将所有页面成功重定向到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

FYI The second block of redirects is required for Wordpress. 仅供参考Wordpress需要第二个重定向块。 I also cannot combine them into one single block or Wordpress will overwrite my changes. 我也无法将它们合并为一个块,否则Wordpress将覆盖我的更改。

When rewriting is configured in .htaccess context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match. .htaccess上下文中配置重写时,应用最后一条规则后,整个过程将重新开始,使用内部重写的URL作为新的“输入”,直到没有其他规则匹配。

That means, when you request /wc-api/v2 , it is not rewritten to HTTPS, because it fails your first RewriteCond , and so the rule in the next block rewrites it to /index.php internally. 这意味着,当您请求/wc-api/v2 ,不会将其重写为HTTPS,因为它会使您的第一个RewriteCond失败,因此下一个块中的规则/index.php内部重写为/index.php

Now the “next round” starts, with /index.php as input. 现在,“下一轮”开始,输入/index.php RewriteCond %{REQUEST_URI} !^/wc-api/v2 now does result in “true”, because the REQUEST_URI is not /wc-api/v2 any more, it is now /index.php . RewriteCond %{REQUEST_URI} !^/wc-api/v2现在确实为“ true”,因为REQUEST_URI不再是/wc-api/v2 ,所以现在是/index.php Therefor, the RewriteRule is applied – and you are redirected to https://example.com/index.php 因此,将应用RewriteRule –并将您重定向到https://example.com/index.php

To avoid this, you must add another RewriteCond , which will prevent the rule from being applied for the REQUEST_URI /index.php as well: 为了避免这种情况,您必须添加另一个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]

I got an error on my server with the suggestion above so modified it slightly and found that this worked for me: 我的服务器上出现了上述建议的错误,因此对其稍加修改,发现这对我有用:

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

Hope it works for you. 希望对你有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM