PDA

View Full Version : Non www to www PHP redirect



MktgPro
05-15-2007, 06:45 PM
Hi - Hoping some of you wizards can help me help a friend.

I need to redirect the non-www version of a domain name to the www version for a site hosted on a Windows server using both PHP and ASP pages.

I've found examples like this one:
----------------------------------------

To Redirect http://www.yourdomain.com/index.php to http://www.yourdomain.com.

This is a 301 redirect using PHP program.

<?
$_SERVER[HTTP_HOST] = strtolower($_SERVER[HTTP_HOST]);
if($_SERVER[HTTP_HOST]=="actiononline.biz" or
$_SERVER[REQUEST_URI]=="/index.php") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http:/ /www.actiononline.biz");
exit();
}
?>
__________________________

But, 1) I'm not sure if this would work for a non-www to www, and 2) I don't know what to put in the variable areas if it does, and 3) I don't know where to put the code on the site, and 4) I don't know if I need to do something different for the ASP pages.

If anyone has any answers to the above, I'd be grateful!

Kathleen

studiokraft
05-15-2007, 10:21 PM
I need to redirect the non-www version of a domain name to the www version for a site hosted on a Windows server using both PHP and ASP pages.

The best solution would be to correct the URLs on the server level. If the server is running Apache, then mod_rewrite can be used, or a Redirect statement in the .htaccess file may be used.

Failing that, this sloppy bit of code might work for the PHP pages:


if (!strpos('|||' . $_SERVER['HTTP_HOST'],"www") && !isset($_REQUEST['rd'])) {
$redirect = "http://www.theserver.com";
if (isset($_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI'])>0) {
$redirect .= $_SERVER['REQUEST_URI'];
}
if (strpos($redirect,"?")) {
$redirect .= "&rd=1";
} else {
$redirect .= "?rd=1";
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $redirect);
}

Hope this helps,

MktgPro
05-15-2007, 11:09 PM
Studiokraft,

Thanks for the response. The domain is hosted on a Windows server and I have no experience with those which is what has me in this mess to begin with.

I know I might be a bit dense, but in the code you gave me, am I right in assuming that the only thing I would need to change is where you had "theserver.com" - I should change that to the site's domain name?

And, where would this code go? on each page? between the <head> tags?

If I've got it right, I'll certainly give it a try!






if (!strpos('|||' . $_SERVER['HTTP_HOST'],"www") && !isset($_REQUEST['rd'])) {
$redirect = "http://www.MY-DOMAIN-NAME.com";[/b]
if (isset($_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI'])>0) {
$redirect .= $_SERVER['REQUEST_URI'];
}
if (strpos($redirect,"?")) {
$redirect .= "&rd=1";
} else {
$redirect .= "?rd=1";
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $redirect);
}

studiokraft
05-15-2007, 11:19 PM
The domain is hosted on a Windows server

Ugh.



am I right in assuming that the only thing I would need to change is where you had "theserver.com" - I should change that to the site's domain name?

Correct. I tried the code out on another site and it seemed to work okay, but it hasn't been tested on all environments, using all possible scenarios, etc. etc. etc. As I said, it is sloppy. ;)


And, where would this code go? on each page? between the <head> tags?

The sites that we design use common files to set things like the page header, so it wasn't an issue to test it on one of those sites. If your friend's site does not have a file that is called with each page request (like a header.php file or something along those lines), then yes, the script would need to be on every page, would have to be near the top of the code before anything is sent to the browser, and the page would need to go through the PHP parser in order for the code to work properly. (If the files on the web site are .html files, the server needs to be told to look for PHP tags in these files)

Given all these variables, a JavaScript solution may be the better route as you wouldn't have to worry about server platforms and programming languages and so forth, but I guess the real question is why would you want to do this in the first place? ;) Does the non-www domain not work for some reason? There may be a DNS issue that needs to be corrected so that you won't have to go through the hassle of redirection.

MktgPro
05-15-2007, 11:38 PM
Thanks again!

I might just give this up. My friend won't change hosts to an Apache server like normal people have :-), and his host told him something about redirecting the non-www would mess something up. So, I'm getting no help from them.

I'm concerned about it because both domains show a 200 status, and I'm afraid the search engines won't like that.

And, for some odd reason, in IE6, if I'm on the non-www version, the drop down menu doesn't work. I think it might be because the tabs with submenus on the main menu have a relative URL of /# and for some reason that causes a page error on IE6, tho it works on the www version in IE6, and it works both ways in IE7.

I may just be too far out of my knowledge zone to be helpful. I'll suggest that he ask his web developer if header.php files are being used, or if he knows of a javascript solution.

I was hoping there would be an easy answer where we could just pop in some code, but I should have known better.

Thanks for your help!

Kathleen

studiokraft
05-18-2007, 03:36 PM
I guess the real question is why would you want to do this in the first place?

Oddly enough, an answer to my own question manifested itself shortly after these posts. A client of ours has a form that is on an SSL connection, and their certificate is specifically for the "www" subdomain. This redirect script ( a cleaner version) was required to insure that the visitor was on the proper URL in order for the Verisign seal to appear.

Vectorman211
05-21-2007, 04:07 PM
It's too easy man. I do it all the time:



if ($_SERVER['REQUEST_URI'] != 'www.domain.com'){
header("Location: https://www.domain.com");
}

//--use this to require SSL
if ($_SERVER['REMOTE_PORT'] != 443){
header("Location: https://www.domain.com");
}


True story!

wige
05-23-2007, 05:57 PM
There is a known issue in PHP where if you have Location: in the header function, it always sends a response code of 302, even if you specify another response code. For example,

header("http//mysite.com/page.html", 301)
will generate a 302 Temporary redirect. The correct code to use is

header("HTTP/1.1 301 Moved Permanently");
header("http//mysite.com/page.html", 301);
I am not sure if this issue has been fixed in newer versions of PHP, but definitely something to check where you use header() to perform redirects.