Forum Moderators: phranque

Message Too Old, No Replies

.htaccess help please

         

oscmss

1:19 am on Apr 15, 2009 (gmt 0)

10+ Year Member



Ok I am a novice when it comes to this stuff. My guy I had working on getting one of my sites up and running bailed out and I am trying to finish it off myself.

I am trying to figure out how to get .htaccess to put certain areas of my site in to [....] I already ahve the SSL cert installed and on a dedicated IP.

For example I want my entire admin area to go to SSL. mysite.com/admincp/

Then I want certain pages like mysite.com/register.php to go to SSL. Then there are account pages that I would like as well.

Then I need it to go back to http:// when the user leaves thoughs pages to other areas of the site

Any help on this please.

jdMorgan

1:44 am on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need a set of directives to force https if the URL-path starts with "/admincp" or is "/register.php" (or any of your other specific SSL files) and the current server_port is not already port 443.

You will need a second set of directives to force http if the URL-path does NOT start with "/admincp" and is NOT /register.php (or any of your other specific SSL files) and the current server_port is not already port 80.

You can do this in various ways, for example, using <Directory> containers in separate SSL/non-SSL <VirtualHost> containers in your server configuration file(s), or using mod_rewrite in .htaccess files.

Basically, you'll need a list of all SSL-only directory URL-paths, plus the all of the special SSL-only page URL-paths such as your register.php page. Switch to https for any of those, and back to http if none of those.

In addition, all objects 'shared' between SSL and non-SSL pages will need to be accessible with either protocol, so that you don't get 'mixed secure and insecure content' warnings. Typically, these include logo and page-template images, CSS files, and external JavaScript files. The easiest way to do this is to link to or include those objects using page-relative or server-relative URLs on your pages, so that the protocol and domain are not specified in the links and therefore stay 'as they were' when the page was loaded.

We have many previous threads here on redirecting between SSL/https and non-SSL/http. Try searching [webmasterworld.com] for those words. Add port 80 and 443 to the search terms if you want to narrow down the results a bit more.

Please review the results, see the references in our Apache Forum Charter and post your best-effort code as a basis for further discussion.

Jim

oscmss

2:01 am on Apr 15, 2009 (gmt 0)

10+ Year Member



This is what I have so far but i am not sure of the code to go back to http://

RewriteEngine On
#
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} admincp
RewriteRule ^(.*)$ https://www.example.com/admincp [R,L]
#
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} /register.php
RewriteRule ^(.*)$ https://www.example.com/register.php [R,L]
#
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} /account.php
RewriteRule ^(.*)$ https://www.example.com/account.php [R,L]

[edited by: jdMorgan at 3:06 am (utc) on April 15, 2009]
[edit reason] Changed to example.com, fixed formatting. [/edit]

jdMorgan

3:04 am on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Close, but you only need two rules:

RewriteEngine On
# Redirect non-https requests for SSL-only URL-paths to https
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/(admincp/?¦register\.php$¦account\.php$)
RewriteRule ^(.+)$ https://www.example.com/$1 [R=301,L]
#
# Redirect https requests for non-SSL URL-paths to http
RewriteCond %{SERVER_PORT} !^80$
RewriteCond %{REQUEST_URI} !^/(admincp/?¦register\.php$¦account\.php$)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

At the beginning of regex patterns in mod_rewrite, "!" means "Not."

Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

oscmss

3:21 am on Apr 15, 2009 (gmt 0)

10+ Year Member



Worked like a charm and I was able to add my other pages to it. Thank you very much