PDA

View Full Version : Default Address as https


Jamie
January 19th, 2006, 15:52
I installed an SSL certificate for one of our reseller customers a few days ago.

He called me today, and would like the secure site to be the default site when a visitor visits www.domain.com

I have tried using a simple .htaccess redirect, to redirect the visitors to the secure URL, but that doesn't seem to work. The load time seems to be very slow when doing it like this.

Is there any other way of doing it?

In a nutshell, I need www.domain.com or http://domain.com or domain.com, or http://www.domain.com to go to https://domain.com without taking a long time (Using a standard redirect, I was waiting for over a minute, and then finally gave up).

Corey Bryant
January 19th, 2006, 16:41
For ASP
<%
dim servPro
servPro = Request.ServerVariables("HTTPS")

if servPro = "off" then
response.redirect "https://www.example.com/"
response.end
end if
%>I am not certain about PHP thought. Can you translate? :)

Jamie
January 19th, 2006, 16:43
I know about as much PHP as my pet sheep knows ASP. :lol: Maybe one of our PHP experts will jump in with their advice and/or translation.

Thanks Corey!

Cameron
January 19th, 2006, 16:46
<?php
$https_url = 'https://therighturl.tld';
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'off' )
{
header('location: ' . $https_url);
exit;
}
else if ( !isset( $_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] == 443 )
{
header('location: ' . $https_url);
exit;
}
?>

Might give it a try.

Jamie
January 19th, 2006, 16:47
Thanks Cameron. Would I just put that in any page I want redirecting to it's https equivalent?

Cameron
January 19th, 2006, 16:49
Yep.

Corey Bryant
January 19th, 2006, 16:51
BTW I also found Using Apache's RewriteEngine to redirect requests to other URLS and to https:// (http://www.whoopis.com/howtos/apache-rewrite.html) - I have no idea if PHP or this is better.

Jamie
January 19th, 2006, 16:57
I'm afraid that didn't work, Cameron. I inserted that PHP (obviously changing the URL to the correct URL) above all other content in index.php, but it had no effect.

BTW I also found Using Apache's RewriteEngine to redirect requests to other URLS and to https:// - I have no idea if PHP or this is better.

That does look quite complicated, but may be exactly what needs to be done. Thanks Corey. A PHP method would be much easier, if anybody knows how to use PHP to achieve this then that would be great, otherwise, I may have to take a close look at the instructions on the site Corey gave.

Cameron
January 19th, 2006, 17:01
Hrm... heh, yeah it wouldn't work. I kind of n00bed. :blush:
<?php
header('Location: https://thesslurl.tld');
exit;
?>
Will work. :)

Jamie
January 19th, 2006, 17:04
That had the same effect as the standard redirect Cameron. It just hung, without really doing anything. :(

Cameron
January 19th, 2006, 17:06
In that case, it may be the http daemon.

Corey Bryant
January 19th, 2006, 17:11
I also found
if ($_SERVER["SERVER_PORT"] != 443)
{
header('Location: https://www.example.com');
}I did some googling on it actually and found some threads that complained actually even about some particular browsers.

Jamie
January 19th, 2006, 17:17
That worked perfectly! Thanks Corey. :D

Corey Bryant
January 19th, 2006, 18:01
Glad to have helped - I looked at about 20 sites before finding that one. Now I know the code too just in case :)

Nasrajan
January 19th, 2006, 18:23
BTW I also found Using Apache's RewriteEngine to redirect requests to other URLS and to https:// (http://www.whoopis.com/howtos/apache-rewrite.html) - I have no idea if PHP or this is better.

You can use this in your .htaccess :

The following rule rewrites your URL http://domain.com to https://domain.com. Also if you try to access http://domain.com/mydir/index.html, it will be redirected to https://domain.com/mydir/index.html. Reduces code overhead :)


RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]


This should work.

LeapingJudas
February 6th, 2007, 05:37
An option that will allow you to use as an include on any page that needs to be redirected (say in iframes or similar) and save you from having to provide the absolutes yourself is:

<?

$leaping = $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'];

$judas = "https://" . $leaping;

if ($_SERVER["SERVER_PORT"] != 443)
{
header('location: ' . $judas);
}

?>

inimino
February 6th, 2007, 06:41
I would recommend putting one simple rewrite rule in your Apache configuration, which can redirect an entire site. This is much more maintainable than sticking PHP all over the place, and doesn't involve you having to touch your hosting customer's files.

Nasrajan's approach looks good.

It's also possible to use a normal Apache redirect if you ensure that it's only redirecting requests that come in on port 80 (usually this depends on your VirtualHost settings, which some control panels get exactly wrong.)