Forum Moderators: phranque

Message Too Old, No Replies

How to force SSL in a directory on our website

         

man0warr

9:29 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



Hi all,

New to this forum, found a solution to another forum here but I have another one I though I would post.

What code in .htaccess would I change so when we go to a directory in our website, like [thisdomain.com...] you will be forced into SSL? This site has a valid SSL cert and can be reached via https://

Thanks for the help in advance.

man0warr

4:10 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



Anyone that can help? I've seen some other topics about similar problems but they wern't exactly what I was looking for.

jdMorgan

4:15 pm on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A search on WebmasterWorld [google.com] will turn up several previous threads to get you started.

Jim

man0warr

6:06 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



Yeah I actually figured it out, but I have a more specific problem now. I made the directory force SSL fine, with:

RewriteEngine On
RewriteCond %{SERVER_PORT}!443
RewriteRule ^(.*)$ [mywebsite.com...] [R,L]

But what I'd really like to do is make a certain file within the directory force SSL, and not the whole directory.

i.e.: [mywebsite.com...]

If I use that website in the above code, the whole directory still gets https along with the .php page, how do I make it so just the .php form is ssl?

jdMorgan

7:10 pm on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before using mod_rewrite, it's a very good idea to read the documentation [httpd.apache.org]. Otherwise, you're asking us to tell you how to fly a jet fighter (or pursue some other dangerous activity) safely with just a short forum post...

In this case, your ".*" pattern is non-specific, and will accept any requested URL in that directory, so all URLs will be redirected. All you have to do is to make the pattern specific to the registration.php URL-path, and include that path in the substitution as well:


RewriteEngine on
RewriteCond %{SERVER_PORT} !443
RewriteRule ^registration\.php$ https://mywebsite.com/directory/registration.php [R=301,L]

Jim

man0warr

7:34 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



Thank you jd, that worked perfectly!

Sorry about the unspecific post, I am still learning Apache and mod_rewrite, thanks for the documentation link also.

foo4704

7:51 pm on Sep 22, 2006 (gmt 0)

10+ Year Member



If you use Apache 2, you can check the variable HTTPS. Also, there's no need to write the domain name hardcoded into the rewrite rule.

This should work (when put in the document root):

RewriteCond %{HTTPS} ^off$
RewriteRule ^(directory/registration\.php)$ https://%{HTTP_HOST}/$1 [R=301,L]