如何使用 htaccess 仅在某些页面上启用 https?
我有一个电子商务网站,我想仅在位于 https://mysite.com 的网站的电子商务部分启用 https /buy
由于我页面上的所有链接都是相对的,因此当有人访问 http://mysite.com 时并点击“购买”,他们将被带到 http://mysite.com/buy
另外,如果他们访问 https://mysite.com/buy 并点击另一个页面的链接,它们将被带到 https://mysite.com。
我只希望在该部分使用 https 的原因是因为我有无法通过 https 发送的外部元素(即 Google 地图、Youtube、Twitter 等)。
有没有办法使用 htaccess 使 /buy 目录强制使用 https,但每个其他页面强制使用 http?
编辑: 如果有人感兴趣,我可以使用 PHP 解决这个问题。我仍然更喜欢 htaccess 解决方案,但这现在可以使用:
if($_SERVER['HTTPS'] == "on") {
if(strpos($_SERVER['REQUEST_URI'],"buy") === false) {
Header("Location: http://$_SERVER['HTTP_HOST']."".$_SERVER['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']");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 .htaccess 文件中尝试此操作:
R=301
将使用 https 状态 301 进行重定向L
将制定最后一条规则NE
用于无转义查询字符串QSA
将附加您现有的查询参数NC
用于忽略大小写比较$1
是您的 REQUEST_URITry this in your .htaccess file:
R=301
will redirect with https status 301L
will make last ruleNE
is for no escaping query stringQSA
will append your existing query parametersNC
is for ignore case comparison$1
is your REQUEST_URI我没有实践经验,但从我看来,htaccess 配置文件应该只影响存储该文件的文件夹中的文件。
因此,您应该能够执行以下操作:
http://www.besthost ratings。 com/articles/force-ssl-htaccess.html
并将其放入您网站的 /buy 文件夹中。
I don't have hands on experience, but from what I see, it looks like the htaccess configuration file should impact only the files in the folder in which the file is stored.
So you should be able to do something like this:
http://www.besthostratings.com/articles/force-ssl-htaccess.html
And put it in the /buy folder of your site.
如果您的网页托管在
9001
端口上,只需启用 Linux 机器上的任何端口并在/etc/httpd/conf.d/ssl.conf
中进行这些更改。然后将侦听端口设置为9002
并创建 SSL 证书和密钥,并将以下配置放入您的 httpd.conf 文件中,您的 .htaccess 文件应如下所示
If your web page is hosted on
9001
port just enable any port on your linux box and make these changes in/etc/httpd/conf.d/ssl.conf
.Then set your Listen Port to9002
and create SSL certificate and key and put following configuration in your httpd.conf fileand your .htaccess file should look like this