Russ
Russ

Reputation: 3956

How can I conditionally redirect to https using mod_rewrite?

I would like to redirect to https using mod_rewrite only if certain conditions are met:

If the URL does NOT contain the word 'administrator' AND the URL DOES contain the string 'xyz' (in any part of the URL, including the querystring)

This does not seem to work:

RewriteCond %{REQUEST_URI} xyz [NC,OR]
RewriteCond %{QUERY_STRING} xyz [NC]
RewriteCond %{REQUEST_URI} !administrator [NC]
ReWriteCond %{HTTPS} != on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]

Upvotes: 2

Views: 426

Answers (2)

Russ
Russ

Reputation: 3956

I ended up using a coding solution as I could not get it to work with mod_rewrite. :(

Upvotes: 0

Gumbo
Gumbo

Reputation: 655239

Try this rule:

RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Testing the request line in THE_REQUEST is easier as it contains both the path and query. But make sure your xyz is not part of the method or HTTP version.

Upvotes: 1

Related Questions