简体   繁体   English

将特定文件夹重定向到HTTPS和Wordpress

[英]Redirect specific folder to HTTPS and Wordpress

I know this question was asked so many times on SO, and I found all the relevant answers to my post, but my situation is a bit more unique, therefore I couldn't use the given answers as solution for my problem. 我知道这个问题在SO上问了很多遍了,我找到了帖子的所有相关答案,但是我的处境更加独特,因此我无法使用给定的答案作为问题的解决方案。

I have a website http://www.example.com which has a folder with the name: secure. 我有一个网站http://www.example.com ,该网站的文件夹名为:安全。 When a user tries to go to http://www.example.com/secure , he should be redirected to the HTTPS version of the site. 当用户尝试访问http://www.example.com/secure时 ,应将其重定向到网站的HTTPS版本。 Now for the tricky part - on the secure page there is only HTTPS links, as requested by every SSL certificate. 现在,对于棘手的部分-在安全页面上,根据每个SSL证书的请求,只有HTTPS链接。 specifically there is a link to https://www.example.com - but when a user clicks it, he should be redirected to the HTTP version of the site. 特别是有一个指向https://www.example.com的链接-但是,当用户单击它时,应该将他重定向到该网站的HTTP版本。 as mentioned - the main page is not secure. 如前所述-主页不安全。

I figured I would need two Rewrite rules: 我认为我需要两个重写规则:

  1. Redirecting everyone trying to get to http://www.example.com/secure to HTTPS protocol. 将尝试将http://www.example.com/secure的所有人重定向到HTTPS协议。
  2. Redirecting everyone trying to get to https://www.example.com to HTTP protocol. 将尝试访问https://www.example.com的所有人重定向到HTTP协议。

This is what i have now in .htaccess in the root directory of the domain based on another question I found here. 这是我现在在域的根目录中的.htaccess中基于基于此处发现的另一个问题的内容。

RewriteEngine On
RewriteBase /

# Turn SSL on for /secure
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but /secure
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/secure
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

This works fine for redirecting the secure page to HTTPS, but I can't figure out why it's not redirecting https://www.example.com to HTTP. 对于将安全页面重定向到HTTPS而言,此方法很好用,但我不知道为什么它没有将https://www.example.com重定向到HTTP。

Also worth mentioning, I have the following code in the same .htaccess that I can't remove. 同样值得一提的是,我在无法删除的.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

Try your rules based on THE_REQUEST variable instead of REQUEST_URI . 尝试基于THE_REQUEST变量而不是REQUEST_URI规则。 THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules , unlike REQUEST_URI variable. REQUEST_URI变量不同, THE_REQUEST变量表示Apache从您的浏览器接收到的原始请求,并且在执行某些重写规则后不会被覆盖

RewriteEngine On
RewriteBase /

# Turn SSL on for /secure
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /secure [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Turn SSL off everything but /secure
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/secure [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Also better to test this in a new browser after clearing browser cache. 清除浏览器缓存后,最好在新的浏览器中进行测试。

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

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