简体   繁体   中英

Apache htaccess redirect to https all urls except certain one with various parameters

I want to redirect to HTTPS all urls which are not secure, except a custom url which includes various parameters.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/code-validator/?code=
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

So http://example.com/code-validator/?code=123 or any parameter on this url will not be redirected to HTTPS.

Use THE_REQUEST instead of REQUEST_URI to be able to:

  1. Match query string since REQUEST_URI only matches URI without query string
  2. Get access to unmodified original URL as received by Apache

Your rule can be:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} !/code-validator/\?code= [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Make sure to keep this rule as your topmost rule and also clear old browser cache.

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