如何仅在使用htaccess的某些页面上启用https?
我有一个电子商务网站,我只想在位于https://mysite.com/buy如何仅在使用htaccess的某些页面上启用https?
因为所有我的网页上的链接网站的电子商务部分启用HTTPS是相对的,当有人访问http://mysite.com和点击购买,他们被带到http://mysite.com/buy
此外,如果他们访问https://mysite.com/buy并点击链接到另一页,他们被带到https://mysite.com。
我只希望HTTPS的原因是因为我有外部元素(即Google地图,Youtube,Twitter等),无法通过https发送。
有没有一种方法,我可以使/ 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']");
}
}
在.htaccess文件试试这个:
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
将HTTPS状态重定向301 L
将最后一条规则NE
是无法回避的查询字符串QSA
将追加现有查询参数NC
用于忽略大小写比较
$1
是你REQUEST_URI
我没有实际操作经验,但是从我所看到的,它看起来像htaccess的配置文件应仅影响文件存储文件的文件夹。
所以,你应该能够做这样的事情:
http://www.besthostratings.com/articles/force-ssl-htaccess.html
而且把它放在你的网站的/买文件夹。
谢谢你的回答。问题是/ buy是重写的URL,所以/ buy文件夹实际上并不存在。 – Jarred 2011-04-19 17:58:49
如果你的网页上9001
端口托管只是让你的Linux系统中的任何端口,使这些变化在/etc/httpd/conf.d/ssl.conf
。那么设置你的监听端口,能够9002
和创建SSL证书和密钥,并把下面的配置您的的httpd.conf文件
Listen 9001
<VirtualHost *:9001>
ServerAdmin [email protected]
DocumentRoot /mnt/work/httpd
<Directory "/mnt/work/httpd">
Options FollowSymLinks
AllowOverride AuthConfig
</Directory>
SSLEngine On
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP
SSLCertificateKeyFile /etc/httpd/www.test.example.com.key
SSLCertificateFile /etc/httpd/www.test.example.com.crt
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.test.example.com:9002%{REQUEST_URI}
和.htaccess文件应该是这样的
AuthType Digest
AuthName "Protected"
AuthDigestProvider file
AuthGroupFile /dev/null
AuthUserFile /mnt/work/httpd/digest_auth
Require user username**
您可以将'RewriteRule^/?(。*)$ http://mysite.com/$1 [R = 301,QSA,L,NE]'简化为'RewriteRule(。*)http://mysite.com$1 [R = 301,QSA,L,NE]' – elimisteve 2013-09-06 20:46:03
@elimisteve:当然可以做到。 – anubhava 2013-09-06 20:50:20