Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm rewriting query strings to pretty URIs, example: index.php?q=/en/contact becomes /en/contact and all works nicely..

# httpd.conf

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Is it even possible to rewrite single queries to force https and force everything else onto http? I've tried many different approaches that typically end in infinate loops. I could write a plugin to do this in PHP but figured it would be more effecient to handle this in the server conf. I'd be greatful for any advice.

EDIT: To clarify, I'd like to be able to rewrite the non SSL to the SSL enabled and every query that is not /en/contact get written to

share|improve this question
up vote 5 down vote accepted

htaccess example:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^en/contact$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTPS} =on
RewriteRule !^en/contact$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

http://domain/en/contact redirect to https://domain/en/contact rewrite to index.php?q=en/contact

http://domain/en/fo rewrite to index.php?q=en/fo

https://domain/en/fo redirect to http://domain/en/fo rewrite to index.php?q=en/fo

https://domain/en/contact rewrite to index.php?q=en/contact

share|improve this answer
The preferred way to check for SSL is HTTPS variable ex. RewriteCond %{HTTPS} !=on – Ryan Gibbons Jul 1 '12 at 15:32
Yes that works, but the issue still remains, all links followed from remain on SSL, I need a way of forcing all other queries back to HTTP without getting stuck in an infinate loop. – Matt Jul 1 '12 at 15:33
I corrected the rules – jnizjo Jul 1 '12 at 15:51
That works really well, many thanks! Sorry I can't upvote yet (not enough rep) – Matt Jul 1 '12 at 15:59
This makes it a little more portable: RewriteRule ^(.*contact$|sign-in)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] – Matt Jul 1 '12 at 16:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.