htaccess 重定向到 https 已重写 URL

htaccess redirect to https for an already Re-Written URL

这是我的 htaccess

Options +FollowSymLinks
RewriteEngine On

# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [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 规则中使用正则表达式替换:

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

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