setting a 301 redirect in the header?

Discussion in 'Site & Server Administration' started by InnovationZen, Oct 2, 2006.

  1. #1
    I am having a hard time with my host to set up a 301 redirect. They do not support .htaccess, so I am talking with the customer service to see how we can solve the problem.

    one solution they proposed me is to put the code below on the header.

    <?

    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.newdomain.com/newpage/newurl.htm");
    exit();

    ?>

    now, does anyone familiar with php or redirects know if that will create a 301 even for the purposes of google optimization and PR?

    thanks
     
    InnovationZen, Oct 2, 2006 IP
  2. MrX

    MrX Well-Known Member

    Messages:
    1,563
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Yes that's a correct php redirect.
     
    MrX, Oct 2, 2006 IP
  3. Jean-Luc

    Jean-Luc Peon

    Messages:
    601
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    After you have changed the code, you can check it with the redirect tool in my signature.;)

    Jean-Luc
     
    Jean-Luc, Oct 3, 2006 IP
  4. InnovationZen

    InnovationZen Well-Known Member

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    108
    #4
    ok, but how do I make the www.domain redirect to the one without www?

    because they use the same index.php file don't them? therefore I am not sure where to place the mentioned code...
     
    InnovationZen, Oct 3, 2006 IP
  5. hadrick

    hadrick Banned

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You just need to place the code at the top of your index.php file:
    <?
    
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.newdomain.com");
    exit();
    
    ?>
    
    Code (markup):
    But it is better to do that through .htaccess as it will do for all of your directoires under the root public folder.

    And yes they use the same index.php file :)

    Regards
     
    hadrick, Oct 4, 2006 IP
  6. jbladeus

    jbladeus Peon

    Messages:
    485
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    can anybody tell me how to do the same in IIS/Asp environment?
     
    jbladeus, Oct 8, 2006 IP
  7. Mike Seiler

    Mike Seiler Peon

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you must use PHP to add or remove the www, I changed the code a little bit below. It would be best if you put it in a header file that's included on all pages.

    In both examples, I used the SERVER_NAME variable so you don't even need to modify the code for your domain. Both will check for www before redirecting, to avoid a loop or execute unnecessarily.

    I also added the REQUEST_URI on the end of the redirect, so it will work on any page. That could come in handy if you already have mixed inbound links to internal pages.

    This one will redirect non-www to www, if it's not already there.
    
    <?php
    
    if(substr($_SERVER['SERVER_NAME'], 0, 4) != 'www.') {
       header("HTTP/1.1 301 Moved Permanently");
       header("Location: http://www.{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}");
       exit;
    }
    
    ?>
    
    Code (markup):
    And this will remove www:

    
    <?php
    
    if(substr($_SERVER['SERVER_NAME'], 0, 4) == 'www.') {
       $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
       header("HTTP/1.1 301 Moved Permanently");
       header("Location: http://$domain{$_SERVER['REQUEST_URI']}");
       exit;
    }
    
    ?>
    
    Code (markup):

    Hope that helps. Personally, though... I can't picture using a host without .htaccess.
     
    Mike Seiler, Oct 9, 2006 IP
    MrX likes this.
  8. InnovationZen

    InnovationZen Well-Known Member

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    108
    #8
    thanks man, very helpful

    i also did find a wordpress plugin that implements the php code automatically, pm me if you need it
     
    InnovationZen, Oct 10, 2006 IP