Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

# httpd.conf

# HANDLE THE QUERY
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 http://example.com/index.php?q=/en/contact to the SSL enabled https://example.com/en/contact and every query that is not /en/contact get written to http://example.com/...

share|improve this question
add comment

1 Answer 1

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 https://domain.com/en/contact 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
add comment

Your Answer

 
discard

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.