View Full Version : PHP Redirect to www.

02-10-2011, 05:55 PM
1) CODE TITLE: Simple Redirect


3) DESCRIPTION: Redirect a user to the www. of your page so cookies will function correctly regardless of how they navigated there.


or, ATTACHED BELOW (see #3 in guidelines below):

if (!preg_match('/www\..*?/', $_SERVER['HTTP_HOST'])) {
header("location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']);
header("location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

02-11-2011, 12:16 AM
1. A .htaccess method will be a lot simpler and easier to implement because it doesn't require PHP parsing-- for example, it will work on images and .js files as well. If .htaccess is not available or if this is only meant to apply to certain pages, this is a good alternative. And for some individuals they might not be able to use .htaccess and would find this useful. I'm not sure that's a lot of people though.

2. Instead of PHP_SELF, since this can be a messy value in some circumstances, I recommend REQUEST_URI. That includes the query string and other components that might otherwise be ignored. One is PATH_INFO that only appears in a few rare circumstances. But to catch everything, REQUEST_URI is simpler and makes 4 lines into 1 :)

3. In case the redirect is ignored, it might be worthwhile to add a link to the page and a redirection notice (only displayed if the redirect does not work immediately); and then I'd use exit(); to make sure the real page isn't displayed and that they update their links. Maybe that feature should be optional.

02-11-2011, 03:17 PM
PATH_INFO could be more common than you think, especially in CMSs and other "pretty url" systems. I use it all the time.

02-11-2011, 08:20 PM
I use it often, but it only is common in those indirect circumstances, though CMS isn't something I'd thought of. It's easy to overlook if you're just imagining what might pop up in a URL, though, so that's why I mentioned it. Good point about CMS though.

02-12-2011, 03:52 PM
Thanks, I overlooked the request_uri. The path_info doesn't return anything for me. I'm using this on IIS and apache servers so I can't use the htaccess and I figured this would probably be the easiest way for both rather than making separate configurations. I'm using it in a config file that is included on all pages so if the redirect doesn't work for some reason they should still be on the same page, right (http://site.com/dir/page.php?id=1 redirect fails still at http://site.com/dir/page.php?id=1)? I guess I should use the @ so the user doesn't get an error message if that failure does occur.

New version

if (!preg_match('/www\..*?/', $_SERVER['HTTP_HOST'])) {
@header("location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

02-12-2011, 05:42 PM
PATH_INFO won't return anything unless it's being used. It won't even be set to an empty string. This is why accurately predicting a full URL is difficult. Luckily, I think REQUEST_URI is completely reliable (at least I hope so-- correct me if I'm wrong).

There are two options for .htaccess on IIS:
1. You can actually configure .htaccess to work;
2. You can use the similar webconfig settings, which do the same thing.
For example:

PHP can't replace this because it must be loaded for a .php request, unless you configure PHP to run for every file extension, but then things get very complicated.

Regardless, for what it's doing, the code you have now should work well.

The only remaining question is about the fragment (eg ...url...#value). It probably would be included in the REQUEST_URI if it is actually sent to the server, but it might be lost if the browser doesn't bother to send it-- the fragment is sometimes kept only clientside because it's meant for clientside operations. I'm not sure about the details on this, or whether even .htaccess would be able to do anything about that. Probably not, actually. Maybe the browser would know to keep it in a redirect since it was in the original request, though.

01-16-2012, 05:40 PM
djr33 is right, for a simple no-"www" to "www" redirection, using an .htaccess file is much simpler and takes less processing power than doing it by PHP. The code I normally use looks like this:

RewriteEngine on
RewriteCond %{http_host} ^domainname\.com [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,NC]

There are cases where using PHP makes more sense because you need to validate something in the database or the URI. In those cases the header() function makes sense. Something like:

header("Location: http://www.example.com/");

More about this function here: