How to set 301 redirection in PHP

Discussion in 'Search Engine Optimization' started by rohit_tripathi60, Jun 1, 2009.

  1. #1
    Hi friend pls suggest me how to set 301 redirection in PHP. I have tried searching from internrt net and puted this code but this isnt helpd

    <?php
    if(!stristr($_SERVER["HTTP_HOST"], 'www')){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.otssolutions.com/" . $_SERVER["REQUEST_URI"]);
    exit();
    }
    ?>


    Please tell me what to do and do ineed to se redirection on all pages or setting it for home page will do for everypage?
     
    rohit_tripathi60, Jun 1, 2009 IP
  2. The Rock

    The Rock Well-Known Member

    Messages:
    2,618
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    138
    #2
    What redirect you want, I think you want with or without www redirect. If so then use below code:-

    Add the following four lines to your .htaccess file, replacing 'domain.com' with your site's domain. Remember, you need to be hosted on a Linux server with the Apache Mod-rewrite module enabled.

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
     
    The Rock, Jun 1, 2009 IP
  3. jamesfoster

    jamesfoster Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you just be a bit clear with your code. I mean an example. (Or provide an example)
    I too need to know about this redirect code.
     
    jamesfoster, Jun 2, 2009 IP
  4. The Rock

    The Rock Well-Known Member

    Messages:
    2,618
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    138
    #4
    Suppose your domain name is abc.com, you want when any one can try to open just abc.com then it will automatically redirect to www.abc.com. For implementing this redirect you have to create .htaccess file. File include following code:-

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^abc.com [nc]
    rewriterule ^(.*)$ http://www.abc.com/$1 [r=301,nc]

    After creating the file upload this file on root directory of your FTP. Make sure your apacha server rewrite module is enable.
     
    The Rock, Jun 2, 2009 IP
  5. GMBRDILOS

    GMBRDILOS Well-Known Member

    Messages:
    573
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    130
    #5


    PHP Single Page Redirect

    In order to redirect a static page to a new address simply enter the code below inside the index.php file.

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.newdomain.com/page.html");
    exit();
    ?>


    PHP Canonical Redirect

    The Canonical 301 Redirect will add (or remove) the www. prefixes to all the pages inside your domain. The code below redirects the visitors of the http://domain.com version to http://www.domain.com.

    <?php
    if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) {
    header(’HTTP/1.1 301 Moved Permanently’);
    header(’Location: http://www.’.$_SERVER['HTTP_HOST']
    .$_SERVER['REQUEST_URI']);
    }
    ?>
     
    GMBRDILOS, Jun 2, 2009 IP
  6. rohit_tripathi60

    rohit_tripathi60 Active Member

    Messages:
    303
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6

    thanks for your valuable suggestion, where to put code in server for canonical redirection? do ineed to make any changes in this code or put as it is? pls pls reply
     
    rohit_tripathi60, Jun 2, 2009 IP
  7. Sadie

    Sadie Active Member

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    301 in php


    <?
    header( "HTTP/1.1 301 Moved Permanently" );
    header( "Status: 301 Moved Permanently" );
    header( "Location: http://www.new-url.com/" );
    exit(0); // This is Optional but suggested, to avoid any accidental output
    ?>
     
    Sadie, Jun 2, 2009 IP
  8. petes1980

    petes1980 Peon

    Messages:
    147
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You would need to put the php code in each php file called. I would suggest at the top of the page.

    However, my personal suggestion would be to do it with the .htaccess file as explained by The Rock in a previous reply. This is because you only need to put the code in once in the .htaccess file, and not copy it into each individual php file called. It also means you don't need to worry about remembering to add the code in to new php files you create. Overall a much neater way to manage your redirects IMO.
     
    petes1980, Jun 2, 2009 IP
  9. rohit_tripathi60

    rohit_tripathi60 Active Member

    Messages:
    303
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #9
    <?php
    if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) {
    header(’HTTP/1.1 301 Moved Permanently’);
    header(’Location: http://www.’.$_SERVER['HTTP_HOST']
    .$_SERVER['REQUEST_URI']);
    }
    ?>

    where to put this code? .htaceess file?
     
    rohit_tripathi60, Jun 2, 2009 IP
  10. petes1980

    petes1980 Peon

    Messages:
    147
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    no, that code is php so would need to go at the top of your php files if you choose to use php to perform the redirects.

    If you want to use .htaccess to perform the redirects, use the following code in your .htaccess file:

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 
    Code (markup):
    PHP and .htaccess are 2 DIFFERENT methods for achieving the same result. Use 1 OR the other, not both.
     
    petes1980, Jun 2, 2009 IP
  11. SEOvietnam

    SEOvietnam Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    you can go to cpanel and set 301 rediect for your domain, and server will handle .htaccess for you.
     
    SEOvietnam, Jun 2, 2009 IP
  12. Canonical

    Canonical Well-Known Member

    Messages:
    2,223
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    110
    #12
    I would HIGHLY suggest that you NOT implement 301 redirects for non-www-->www (or visa versa) in your code. If you do it in code, you have to add the code to the top of EVERY page on your site. And going forward you have to remember to add it to all new pages as well.

    It is MUCH more efficient to do this using some CPANEL 301 redirect or doing it directly in Mod Rewrite using .htaccess files. This way you write one RewriteRule in a .htaccess files that takes care of ALL pages on your site (existing and future pages).
     
    Canonical, Jun 2, 2009 IP
  13. jamesfoster

    jamesfoster Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for responding. I think it wont be clear unless I practically do it. Thank you very much.
     
    jamesfoster, Jun 10, 2009 IP