apache .htaccess 301 将所有 https 重定向到 http,除了一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2079015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess 301 redirect for all https to http EXCEPT ONE PAGE
提问by dell116
Here is the code I have currently in my .htaccess file:
这是我目前在 .htaccess 文件中的代码:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I know Hyman about rewrites...and all of the other posts on this subject really don't fit my criteria and I don't know enough about the language to try and decipher it.
我知道 Hyman 是关于重写的……关于这个主题的所有其他帖子都真的不符合我的标准,而且我对语言的了解不够,无法尝试破译它。
Basically what I need is this:
基本上我需要的是这个:
- redirect all instances of "example.com" to "www.example.com"
- redirect all instances of "https//www.example.com" to "http://www.example.com" with the exception of 1 page!!!! (in case it matters, the filename of that page is payments.php)
- 将“example.com”的所有实例重定向到“www.example.com”
- 将“ https//www.example.com”的所有实例重定向到“ http://www.example.com” ,但 1 页除外!!!(以防万一,该页面的文件名是payments.php)
The code I have above works, but for the 1 page that I need to be https, it is rewriting the url to http. That one page has to be https.
我上面的代码有效,但是对于我需要成为 https 的 1 个页面,它将 url 重写为 http。那一页必须是https。
Thanks, Chris
谢谢,克里斯
采纳答案by St.Woland
Apache/2.2.6 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8g PHP/5.2.6
Apache/2.2.6 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8g PHP/5.2.6
I've tested it locally, all use cases seem to work fine. If you have further questions, feel free to ask.
我已经在本地对其进行了测试,所有用例似乎都可以正常工作。如果您还有其他问题,请随时提问。
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Redirect from example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/payments\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/ [R=301,L]
# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/payments\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ [R=301,L]
IMPORTANT!When the user navigates from any httpspage with wwwto any httpspage withoutwww, he's asked to accept security certificate of your non-www domain.
重要的!当从任何用户浏览HTTPS与页面WWW任何HTTPS页面没有WWW,他要求接受你的非www域名的安全证书。
For example (YES = request to accept the certificate, NO - opposite):
例如(YES = 请求接受证书,NO - 相反):
1) https://www.asdf.com/payments.php - YES (www.asdf.com)
2) http://www.asdf.com/phpinfo.php - NO
3) https://asdf.com/phpinfo.php - YES (asdf.com)
4) https://www.asdf.com/phpinfo.php - NO
I tried to reorder rules in .htaccess with no success. If anyone finds a better solution, it'll be highly appreciated.
我试图在 .htaccess 中重新排序规则但没有成功。如果有人找到更好的解决方案,将不胜感激。
回答by jspcal
in addition to william's great solution, you can negate request uri somewhere before https rewriterule
除了 william 的出色解决方案之外,您还可以在 https rewriterule 之前的某处否定请求 uri
RewriteCond %{REQUEST_URI} !^/?payments\.php
回答by Jayapal Chandran
That was a nice post and it seems to redirect to http properly but i want some thing like the following
那是一篇不错的帖子,它似乎正确重定向到 http,但我想要一些类似以下的内容
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule (.*) http://www.site.com/ [R=301,L]
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^products https://www.site.com/products/ [R=301,L]
# Disable SSL on pages other than payments.php
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^products
RewriteRule ^(.*)$ http://www.site.com/ [R=301,L]
# Require SSL on payments.php
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^products\/?$
RewriteRule ^(.*)$ https://www.site.com/ [R=301,L]
RewriteCond !^(index\.php|images|js|css|static|img|payment|robots\.txt|blank.gif)
RewriteRule ^(.*)$ index.php/ [L]
my url is http://www.sitename.comand the trailing forward slash is optional what i need is if there is no text like products in the url then it needs to he http:// and if there is a string products in the uri then it should be https://
我的 url 是http://www.sitename.com并且尾随的正斜杠是可选的我需要的是如果 url 中没有类似产品的文本那么它需要他 http:// 并且如果有一个字符串产品uri 那么它应该是 https://
how to do that... when i tried it is going to a loop... redirection loop.
怎么做...当我尝试它会进入一个循环...重定向循环。
i am using codeigniter frame work and i have removed index.php from the url.
我正在使用 codeigniter 框架工作,并且我已经从 url 中删除了 index.php。
so if something like http://sitename.com/productsoccurred then i want it to be https://sitename.com/products
所以如果发生了类似http://sitename.com/products 的事情,那么我希望它是https://sitename.com/products
and if there no products in the url then it should redirect to http://
如果 url 中没有产品,那么它应该重定向到 http://
回答by William
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Redirect from example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
# Disable SSL on pages other than payments.php
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^payments\.php$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
# Require SSL on payments.php
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^payments\.php$
RewriteRule ^(.*)$ https://www.example.com/ [R=301,L]
I haven't tested it but something like that should work
我还没有测试过,但类似的东西应该可以工作
Edit:Updated
编辑:更新

