Submit Your Article Forum Rules

Results 1 to 8 of 8

Thread: Non www to www PHP redirect

  1. #1
    Senior Member
    Join Date
    Dec 2004
    Posts
    198

    Non www to www PHP redirect

    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
    Getting It Write, Inc.
    The Small Business Marketing Pro
    Real Estate Marketing Assistance: Real Estate Websites - Brands - Logos
    http://www.buildrealestateresults.com

  2. #2
    Junior Member
    Join Date
    Jun 2006
    Posts
    24

    Re: Non www to www PHP redirect

    Quote Originally Posted by MktgPro
    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:

    Code:
    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,
    Please read the forum rules on signatures.

  3. #3
    Senior Member
    Join Date
    Dec 2004
    Posts
    198

    Re: Non www to www PHP redirect

    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!



    Quote Originally Posted by studiokraft
    Code:
    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);
    }
    Getting It Write, Inc.
    The Small Business Marketing Pro
    Real Estate Marketing Assistance: Real Estate Websites - Brands - Logos
    http://www.buildrealestateresults.com

  4. #4
    Junior Member
    Join Date
    Jun 2006
    Posts
    24

    Re: Non www to www PHP redirect

    Quote Originally Posted by MktgPro
    The domain is hosted on a Windows server
    Ugh.


    Quote Originally Posted by MktgPro
    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. ;)

    Quote Originally Posted by MktgPro
    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.
    Please read the forum rules on signatures.

  5. #5
    Senior Member
    Join Date
    Dec 2004
    Posts
    198
    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
    Getting It Write, Inc.
    The Small Business Marketing Pro
    Real Estate Marketing Assistance: Real Estate Websites - Brands - Logos
    http://www.buildrealestateresults.com

  6. #6
    Junior Member
    Join Date
    Jun 2006
    Posts
    24

    Re: Non www to www PHP redirect

    Quote Originally Posted by studiokraft
    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.
    Please read the forum rules on signatures.

  7. #7
    Member Vectorman211's Avatar
    Join Date
    Jul 2006
    Posts
    88

    Re: Non www to www PHP redirect

    It's too easy man. I do it all the time:

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

  8. #8
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    Re: Non www to www PHP redirect

    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,
    PHP Code:
    header("http//mysite.com/page.html"301
    will generate a 302 Temporary redirect. The correct code to use is
    PHP Code:
    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.
    The best way to learn anything, is to question everything.
    WigeDev - Freelance web and software development

Similar Threads

  1. .htaccess, can I redirect a redirect?
    By T-G in forum Google Discussion Forum
    Replies: 3
    Last Post: 01-25-2009, 09:28 PM
  2. 301 redirect
    By hwajcman in forum Google Discussion Forum
    Replies: 9
    Last Post: 08-11-2007, 04:31 PM
  3. 301 redirect
    By rms7design in forum Search Engine Optimization Forum
    Replies: 3
    Last Post: 08-01-2007, 05:37 AM
  4. 301 redirect
    By jasonashby in forum Search Engine Optimization Forum
    Replies: 11
    Last Post: 04-17-2006, 05:23 PM

Posting Permissions

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