Results 1 to 7 of 7

Thread: PHP Redirect to www.

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default PHP Redirect to www.

    1) CODE TITLE: Simple Redirect

    2) AUTHOR NAME/NOTES:

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

    4) URL TO CODE:

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

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

    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (02-11-2011)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PATH_INFO could be more common than you think, especially in CMSs and other "pretty url" systems. I use it all the time.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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
    PHP Code:
    if (!preg_match('/www\..*?/'$_SERVER['HTTP_HOST'])) {
        @
    header("location: http://www." $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

    Corrections to my coding/thoughts welcome.

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    http://stackoverflow.com/questions/2...ivalent-on-iis

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    Code:
    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:

    Code:
    header("Location: http://www.example.com/");
    More about this function here:

    http://php.net/manual/en/function.header.php

    http://www.phpredirection.com/advanced-php-redirection/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •