我怎么能只在某些页面与htaccess的HTTPS? [英] How do I enable https only on certain pages with htaccess?

查看:225
本文介绍了我怎么能只在某些页面与htaccess的HTTPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子商务网站,我只想在位于 HTTPS网站的电子商务部分启用HTTPS:// mysite.com/buy

由于所有对我的网页的链接都是相对的,当有人访问 http://mysite.com 并点击购买,他们被带到 http://mysite.com/buy

另外,如果他们访问 https://mysite.com/buy 并点击一个链接到另一个页面他们被带到 https://mysite.com

我想HTTPS仅在一个部分的原因是因为我有外部因素(如谷歌地图,YouTube,微博等)无法通​​过HTTPS发送。

是否与htaccess的一种方式,我可以使/购买目录力HTTPS,但所有其他网页力HTTP?

编辑: 如果有人有兴趣,我可以解决这个使用PHP。我仍然preFER一个htaccess的解决方案,但是这会为现在的:

 如果($ _ SERVER ['HTTPS'] ==ON){
    如果(strpos($ _ SERVER ['REQUEST_URI'],买)=== FALSE){
        标题(位置:http:// $ _ SERVER ['HTTP_HOST']$ _ SERVER ['REQUEST_URI']);
    }
}
 

解决方案

在你的.htaccess文件试试这个:

 选项+了FollowSymLinks
RewriteEngine叙述上

#重定向HTTP /购买页面
的RewriteCond%{} SERVER_PORT = 80
重写规则^买/?$ https://mysite.com/buy [R = 301,QSA,L,NE]

#重定向HTTPS非/购买页面
的RewriteCond%{} SERVER_PORT = 443
的RewriteCond%{REQUEST_URI}!^ /购买[NC]
重写规则^ /?(。*)$ http://mysite.com/$1 [R = 301,QSA,L,NE]
 

R = 301 将与HTTPS状态301
重定向 将最后一条规则
NE 是无法逃避查询字符串
QSA 将追加现有查询参数
NC 是忽略大小写的比较

$ 1 是REQUEST_URI

I have an ecommerce site, and I want to enable https only on the ecommerce section of the site located at https://mysite.com/buy

Since all of the links on my pages are relative, when someone visits http://mysite.com and clicks on Buy, they are taken to http://mysite.com/buy

Also, if they visit https://mysite.com/buy and click on a link to another page, they are taken to https://mysite.com.

The reason I want https only on that one section is because I have external elements (i.e. Google Maps, Youtube, Twitter, etc) that cannot be sent over https.

Is there a way with htaccess that I can make the /buy directory force https, but every other page force http?

Edit: In case anyone is interested, I was able to solve this using PHP. I would still prefer an htaccess solution, but this will work for now:

if($_SERVER['HTTPS'] == "on") {
    if(strpos($_SERVER['REQUEST_URI'],"buy") === false) {
        Header("Location: http://$_SERVER['HTTP_HOST']."".$_SERVER['REQUEST_URI']");
    }
}

解决方案

Try this in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

# redirect for http /buy page
RewriteCond %{SERVER_PORT} =80
RewriteRule ^buy/?$ https://mysite.com/buy [R=301,QSA,L,NE]

# redirect for https non /buy pages
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} !^/buy [NC]
RewriteRule ^/?(.*)$ http://mysite.com/$1 [R=301,QSA,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
NC is for ignore case comparison

$1 is your REQUEST_URI

这篇关于我怎么能只在某些页面与htaccess的HTTPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆