Regex HttAccess重定向到https以获取已重新写入的URL

Regex HttAccess重定向到https以获取已重新写入的URL,regex,apache,.htaccess,mod-rewrite,redirect,Regex,Apache,.htaccess,Mod Rewrite,Redirect,这是我的访问权限 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} regis

这是我的访问权限

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]
我有一个注册页面,URL如下hxxp://www.example.com/index.php?register htaccess正在正确重定向到HTTPS

现在我还想要我的购买页面(hxxpp://www.example.com/buy)重定向到HTTPS。此页面的真实URL为hxxp://www.example.com/index.php?buy


请告诉我如何修改我的htaccess规则以满足此需求。

您可以在
https
规则中使用regex替换:

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

确保这是htaccess中的第一条规则。

您的URI方案有点混乱。一方面你有
/index.php?register
,另一方面你有
/buy
(它在内部重写为
/index.php?products&type=buy
。为什么没有
/register
(重写为
/index.php?register
)太?@JustinIurman:很抱歉,URL中有一个输入错误。已修复。以前测试过。不起作用。该网站由干净的URL和带有多个查询的长URL组成。也是一个已重写的URL,我希望它是HTTPS。所以我认为{QUERY_STRING}在那种情况下不起作用。如果我错了,请纠正我。检查更新的规则并确保这是我们htaccess中的第一条规则。是的,它正在工作。但我也想使用查询字符串条件。我可以使用以下方法:RewriteCond%{HTTPS}off RewriteCond%{the_REQUEST}/buy[或]RewriteCond%{QUERY STRING}register RewriteRule^HTTPS://{HTTP_HOST}%{REQUEST_URI}[L,NE,R=302]是的,您当然可以使用,但请确保该规则是第一条规则。已将我的htaccess更改为不工作。不确定我的OR条件是否正确。