How to conditionally redirect to https using mod_rewrite?

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
bodgekaloopie
Forum Newbie
Posts: 1
Joined: Tue Sep 21, 2010 1:29 pm

How to conditionally redirect to https using mod_rewrite?

Post 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.
longvnit
Forum Newbie
Posts: 2
Joined: Thu Sep 23, 2010 7:02 am

Re: How to conditionally redirect to https using mod_rewrite

Post by longvnit »

You try it :

Code: Select all

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