简体   繁体   English

htaccess重定向到https以获取已重写的URL

[英]htaccess redirect to https for an already Re-Written URL

This is my htaccess 这是我的htaccess

Options +FollowSymLinks
RewriteEngine On

# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} register
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

# don't do anything for images/css/js etc
RewriteRule \.(gif|jpe?g|png|css|js|woff|map)$ - [NC,L]

# redirect to HTTP for all other pages
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !register
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [PT,L]

I've got a register page with an URL like hxxp://www.example.com/index.php?register and the htaccess is redirecting correctly to HTTPS. 我有一个注册页面,其URL类似于hxxp://www.example.com/index.php?register ,并且htaccess正确重定向到HTTPS。

Now I also want my buy page( hxxpp://www.example.com/buy ) to redirect to HTTPS . 现在,我还希望我的购买页面( hxxpp://www.example.com/buy重定向到HTTPS This page's real URL is hxxp://www.example.com/index.php?buy 此页面的真实URL是hxxp://www.example.com/index.php?

Please advice me on how to modify my htaccess rule to take care of this need. 请为我提供有关如何修改htaccess规则以解决此需求的建议。

You can use regex alternation in your https rule: 您可以在https规则中使用正则表达式替代:

# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(register|buy)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

Make sure this is very first rule in our htaccess. 确保这是我们htaccess中的首要规则

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

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