简体   繁体   中英

htaccess redirect to https for an already Re-Written URL

This is my 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.

Now I also want my buy page( hxxpp://www.example.com/buy ) to redirect to HTTPS . This page's real URL is hxxp://www.example.com/index.php?buy

Please advice me on how to modify my htaccess rule to take care of this need.

You can use regex alternation in your https rule:

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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