CristianC
CristianC

Reputation: 301

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.

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 785108

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.

Upvotes: 1

Related Questions