homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess to redirect to https folder
then back to http:// when leaving folder

 7:56 pm on Nov 8, 2006 (gmt 0)


Because I have a secure ceckout on my site, I have put some code to redirect anything in the /checkout folder to https://domain.com/checkout (currently using a php redirect). However people then tend to browse around my site using https:// which can cause some issues (I cant serve ads on these pages!)

I would be keen to find a nice htaccess rule, which would do the following:

If user enters any page within: /checkout then redirect to https page
if user enteres any page outside of /checkout then redirec to http page

How could I do this?




 8:18 pm on Nov 8, 2006 (gmt 0)

ok got this far:

in /.htaccess

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

in /checkout/.htaccess

RewriteEngine on
RewriteCond %{SERVER_PORT}!^443$
RewriteRule ^(.*)$ https://test.domain.com/checkout/$1 [R=301,L]

This works for 99% of cases, except when you load:

[test.domain.com...] (with no trailing slash or filename)

which then goes to:


Any ideas?


 9:46 pm on Nov 8, 2006 (gmt 0)

No idea if this will help, but there's a serious logic error that needs to be fixed before proceeding. The problem is caused by the fact that the main /.htaccess will be processed for all requests, including requests to /checkout. As a result, the code in /.htaccess needs to know not to rewrite https /checkout requests to http:

In /.htaccess

RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$
RewriteCond $1 !^checkout
RewriteRule (.*) http://test.domain.com/$1 [R,L]



 9:55 pm on Nov 8, 2006 (gmt 0)

Jim, I am not saying you are wrong. But tell me if I am wrong please. Does the child folder .htaccess not overwrite all parent folder commands? What if your host has something in /home folder? Does our .htaccess in our home folder overwrite any settings in there?


 10:47 pm on Nov 8, 2006 (gmt 0)

with that amend it works fine :) Cheers

the only problem is, the images css and js files are rewritten back to http:// because they are not in the checkout folder!

Can we get a slight amend so that it only redirects actual pages rather than the images, etc?

would need to deal with php pages (and obviously domain.com/ where pagename is not shown..,)



 10:52 pm on Nov 8, 2006 (gmt 0)


Overwrite? No. The top-level rewrites will be invoked, followed by the lower-level rewrites, but both will occur. And since an external redirect is involved, that's not good.


You should be able to add an exclusion to the /checkout/.htaccess file, in much the same way as I showed for the /.htaccess exclusion.

I'm not clear on all the exclusions you might need, but the pattern for css and js files would be something like


Note: No start anchor, because we're only looking at the .js or .css suffix, and you must replace the broken pipe "¦" character shown with a solid pipe character from your keyboard; Posting on this forum modifies the pipe characters.



 11:03 pm on Nov 8, 2006 (gmt 0)

Thanks jim

I'll try this. But should the exclusion go in the /.htaccess file, as the images etc are within the /images folder, and it is this htaccess file which is rewriting https > http?



 11:43 pm on Nov 8, 2006 (gmt 0)

I don't know how your site is set up. I encourage you to experiment, though, as this is a discussion forum [webmasterworld.com], not a help desk, and the only way to learn is to try. :)



 3:31 am on Nov 9, 2006 (gmt 0)

Thanks Jim, for clarifying. I appreciate your time.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved