Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect to www version but not https

         

aweb4u

3:50 am on Nov 14, 2008 (gmt 0)

10+ Year Member



I have a website on a shared host running Apache 1.3.39.

This site has a shopping cart and an order form page, and I would like to take advantage of the host's secure certificate for the order form. So on the shopping cart page (http://www.example.com/cart.php) I have the Checkout link going to [host.com...] which encrypts the page.

I would like to rewrite all requests to example.com to www.example.com, so I created an .htaccess file that looks like this:

# Redirect non www.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.co.nz
RewriteRule (.*) http://www.example.co.nz/$1 [R=301,L]

This works fine, but when a user goes to the order form page the URL gets rewritten from [host.com...] to http://www.example.com/orderform.php

Of course this breaks the SSL, so I decided I only want to do the rewrite on non-https URLS. I did some research and found several people saying to add the line:

RewriteCond %{SERVER_PORT} !^443$

So I added this line just before the RewriteRule but that didn't seem to do anything.

I asked my hosting company and they suggested replacing the resently added line with:

RewriteCond %{HTTPS} !^on$

So I tried this but it still doesn't work, the rewrite is still happening.

Does anyone have any ideas on how I can get this to work? I only want to do the rewrite on http and not on https URLs.

g1smd

4:05 am on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First up, I can't see any rewrites in your code. I can see a redirect though.

Next, it says "redirect everything that is *not* exactly www.example.co.nz to www.example.co.nz".

Why not change the Condition to say "redirect when request is only for example.co.nz" instead?

I assume that when you use host.com and example.com in your example that you really do mean that two different domain names are involved.

jdMorgan

4:27 am on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Flush your browser cache completely before testing any new server-side code...

Jim

aweb4u

9:50 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



Ok, I've changed it to:

# Redirect non www.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.nz [NC]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ http://www.example.co.nz/$1 [R=301,L]

And it seems to work ok now.

And yes, example.com and host.com are two different domain names.

g1smd

10:30 pm on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Clarity of question is everything in this game.

Thanks for being (reasonably) clear and concise. :-)