简体   繁体   English

这些重写规则有什么问题? 重定向到https的一页,返回其他所有的http

[英]What is wrong with these rewrite rules? Redirect to https for one page, back to http for all others

Desired behavior: 所需行为:

Visiting http://www.example.com/our-services/individual-medical-dental-insurance redirects to https: https://www.example.com/our-services/individual-medical-dental-insurance 参观http://www.example.com/our-services/individual-medical-dental-insurance重定向到https: https://www.example.com/our-services/individual-medical-dental-insurance

Visiting any other page via https, such as https://www.example.com/about-group-benefit-services/ , redirects to non-https: http://www.example.com/about-group-benefit-services/ 通过https访问任何其他页面 ,例如https://www.example.com/about-group-benefit-services/ ,重定向到非https: http://www.example.com/about-group-benefit-services/ : http://www.example.com/about-group-benefit-services/

I've tried a variety of solutions, and this is my most recent version (which does not work): 我尝试了各种解决方案,这是我的最新版本(不起作用):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^our-services\/individual\-medical\-dental\-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 
[L,R=301]   
</IfModule>

It may be worth noting that this is in a WordPress site, and the remainder of the .htaccess file contains this: 可能值得注意的是,该文件位于WordPress网站中,.htaccess文件的其余部分包含以下内容:

# 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

(The .htaccess does not contain anything other than the above). (.htaccess除以上内容外不包含其他任何内容)。

Not sure if this will make it work but there's a few issues with your rules and it won't fit into a comment: 不知道这是否能使它正常工作,但是您的规则存在一些问题,因此不适合发表评论:

The %{REQUEST_URI} always starts with a "/" and thus your regex will always be true (since it'll never start with "our-services"). %{REQUEST_URI}始终以“ /”开头,因此您的正则表达式将始终为true(因为它永远不会以“ our-services”开头)。

The other thing is that your rule is doing the appending of the URL at the end because of the $1 backreference, so you need to remove that. 另一件事是您的规则由于$1反向引用而在末尾附加了URL,因此您需要删除它。

Lastly, both redirects are going to https:// . 最后,两个重定向都将转到https://

Also, you don't need to escape the slashes or dashes: 另外,您无需转义斜杠或破折号:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]   
</IfModule>

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

相关问题 使用.htaccess,允许通过HTTPS://提供特定的Wordpress页面集,并将所有其他页面重定向回HTTP:// - With .htaccess, allow a specific set of Wordpress pages to be served over HTTPS://, and redirect all others back to HTTP:// 将一个Wordpress页面从HTTPS重定向到HTTP - Redirect one Wordpress page from HTTPS to HTTP 如何将 Http 中的所有帖子/页面重定向到 Https - How to Redirect my all post/page in Http to Https http://到https://重定向,但是某些内容将其覆盖回http:// - http:// to https:// redirect but something is overriding it back to http:// WordPress:网站是HTTPS; 通过HTTP仅重定向一个站点页面 - WordPress: Site is HTTPS; redirect only one site page over HTTP WordPress 将所有 HTTPS 重定向到 HTTP - WordPress redirect all HTTPS to HTTP 通过.htaccess从HTTPS排除一页,但将该页面上的链接重定向回HTTPS - Exclude one page from HTTPS via .htaccess, but redirect links on that page back to HTTPS WordPress将整个站点重定向到HTTPS,然后将一页的重定向规则添加到HTTP - WordPress redirect full site to HTTPS and then add redirect rule for one page to HTTP 将所有 http 请求重定向到 wordpress 中的 https - redirect all http request to https in wordpress Openshift:在.htaccess中将HTTP重写为HTTPS正在进行302重定向,而不是重写 - Openshift: Rewrite HTTP to HTTPS in .htaccess is doing a 302 redirect instead of rewriting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM