Page 1 of 1

How to conditionally redirect to https using mod_rewrite?

Posted: Tue Sep 21, 2010 1:34 pm
by bodgekaloopie
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 work:

Code: Select all

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]
nor does this:

Code: Select all

RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I know little about mod_rewrite - any help would be greatly appreciated.

Re: How to conditionally redirect to https using mod_rewrite

Posted: Thu Sep 23, 2010 7:14 am
by longvnit
You try it :

Code: Select all

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]