Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to switch from HTTP to HTTPS

Big headache !

         

Artesius

8:30 am on Aug 1, 2006 (gmt 0)

10+ Year Member



Hello!
I'm trying to switch between HTTP and HTTPS for one specific page on my website.


RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=formulaire_etat_civil$
RewriteRule ^$ https://%{HTTP_HOST} [QSA,L]

I've tried a lot of combination but none work...
I just want to switch to SSL if the variable page is set to this value.
Eventually I would try to look for every URL and turn it into HTTP if the person is in HTTPS (don't want to ues too much resources on the server!)

Schematically (don't know if this vocable exists!)
If page is set to formulaire_etat_civil switch to HTTPS, in any other case if the protocol is HTTPS switch to HTTP.

If somebody could help :)
I couldn't think more about it after one day and a half... :)
Thanks :)

Some precision: I've read a lot of the post about the mod_reworite but no solutions works.
I've found one trick, which consist to add :SSL in the URL, but it didn't work if the person write the adress without https in the adress bar.

[edited by: Artesius at 8:36 am (utc) on Aug. 1, 2006]

jdMorgan

3:22 am on Aug 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You schematic description included a variable not addressed in your code -- the current HTTP/HTTPS connection state. Also, the case where the requested URL is not "/" was not addressed in the code.
[code]
RewriteEngine on
#
# If URL is "/", query is "formulaire_etat_civil", and current connection is NOT HTTPS, switch to HTTPS
RewriteCond %{QUERY_STRING} ^page=formulaire_etat_civil$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^$ [%{HTTP_HOST}...] [R=301,L]
#
# If URL is "/", query is NOT "formulaire_etat_civil", and current connection is HTTPS, switch to HTTP
RewriteCond %{QUERY_STRING} !^page=formulaire_etat_civil$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^$ http://%{HTTP_HOST}/ [R=301,L]
#
# If URL is NOT "/" and current connection is HTTPS, switch to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteRule . http://%{HTTP_HOST}/ [R=301,L]
[code]
Note that [QSA] is only needed if your rule *appends* a new variable to an existing query string. Otherwise, mod_rewrite's default behaviour is to pass query strings through RewriteRules without change.

Jim

[edited by: jdMorgan at 3:23 am (utc) on Aug. 2, 2006]

Artesius

11:53 am on Aug 3, 2006 (gmt 0)

10+ Year Member



Thanks I'll try it :)

Caterham

7:57 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



RewriteCond %{QUERY_STRING} ^page=formulaire_etat_civil$
RewriteCond %{SERVER_PORT}!^443$
Why are you using a regEx here and not the lexicographically equal (=), which should have a better performance than a regEx has?

RewriteCond %{QUERY_STRING} =page=formulaire_etat_civil
RewriteCond %{SERVER_PORT} !=443

or for empty strings instead of

RewriteCond %{...} ^$ or
RewriteCond %{...} .

this one, created esp. for that case to avoid a regEx:

RewriteCond %{...} =""

is there a special reason?

[edited by: Caterham at 7:58 pm (utc) on Aug. 9, 2006]