Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite Toggle SSL

Using mod_rewrite to toggle SSL on or off

         

doodlebug

2:13 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



I have a site with a SSL key, when vistors reach www.mysite.com/mod.php?name=secure i want to redirect to SLL(https//:www.mysite.com/mod.php?name=secure) If the name parameter changes e.g. name=unsecure it reverts back to [.......]

Any help would be much appreciated.

Cheers

doodlebug

5:11 am on Jul 2, 2005 (gmt 0)

10+ Year Member



Any assistance would be much appreciated.

Ta moochly

jdMorgan

1:45 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



doodlebug,

Welcome to WebmasterWorld!

Code like the following, placed in httpd.conf, might do what you want. I have to warn you though, that working with SSL can be difficult. You will likely need to modify this code (or your planned implementation), unless I understood exactly what you are wanting to do.

Our purpose in this forum is not to write code on demand, but rather to help you learn to write your own code. For this reason, you'll need to study the following code, along with the documentation cited in our Forum Charter [webmasterworld.com], and understand how it works. We can then address your very specific questions in this forum.


RewriteEngine On
# If query_string contains "name=secure"
RewriteCond %{QUERY_STRING} name=secure
# And we're not already on port 443 (secure connection port)
RewriteCond %{SERVER_PORT} [code]!^443$
# Redirect to https for secure connection
RewriteRule ^/mod\.php$ https://www.mysite.com/mod.php [R=301,L]

The original query string will be retained and passed to the secure connection.

Jim

doodlebug

6:20 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



Hi Jim

many thanks for your Welcome and reply - I have been doing a large amount of research since my post and have got to the point now where I am stuck.

The code below is where I have got tooo. Unfortunately it does not seem to work 100% as the second set of code seems to be executed even tho is does not match the condition - any pointers?

RewriteCond %{REQUEST_URI} ^/TEST/secure.html$
RewriteCond %{SERVER_PORT}!^443
RewriteRule ^(.*)$ [mydomain.com...] [R,L]

RewriteCond %{REQUEST_URI}!^/TEST/secure.html$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ [mydomain.com...] [R,L]

Many Thanks.

[edited by: doodlebug at 6:23 pm (utc) on July 8, 2005]

doodlebug

6:22 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



Oops - typo - corrected

jdMorgan

8:30 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Always flush your browser cache before testing changes to your code; You might want to try that now and see if it changes anything.

A useful trick you can use for debugging is to append your variables to the substitution URL as a query string:

RewriteRule ^(.*)$ http://www.example.com/TEST/$1?port=%{SERVER_PORT}&URI=%{REQUEST_URI} [QSA,R,L]

Then if soemthing goes wrong, you can examine the variables to be sure they are what you expected.

Jim

doodlebug

8:03 am on Jul 9, 2005 (gmt 0)

10+ Year Member



Hi

A little further forward but still pulling my hair out - hehe.

RewriteCond %{REQUEST_URI} ^/TEST/secure.html$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)name=Secure(.*)$
RewriteCond %{SERVER_PORT}!^443
RewriteRule ^(.*)$ [mydomain.com...] [R,L]

RewriteCond %{REQUEST_URI}!^/TEST/secure.html$
RewriteCond %{QUERY_STRING}!^(.*)name=Secure(.*)$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ [mydomain.com...] [R,L]

If I comment out the last 4 lines it works perfectly, if I leave them in SLL kicks in but all the images are red crosses - any idea/pointer why the latter conditions mat effect the first four?

Yours now bald! - lol.

jdMorgan

7:54 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, please try the suggestion above to make the server variables visible, and then tell us what you see.

Jim

doodlebug

8:46 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



[mydomain.com...]

If I comment out those last 4 lines from the above the SSL redirect works fine.

Thx again for your time.