Search code examples
phpapache.htaccessheaderhttp-status-code-301

301 redirect - apache or php for my case?


I have a blog www.SITE_NAME.com which is hosted in blogger.com, Its almost 4 year old and have better search engine ranking. Most of the traffic came through Google. Now i am redesigning my site in drupal.

So i want to redirect all older links with a 301 to new pages , Since i have nearly 700 pages , i want some logic to apply (and some case i want to redirect manually) . Which is better, using Apache or php? Or any other suggestion?

Note : since my old site is in blogger.com, its path is something like this www.SITE_NAME.com/2007/08/music.html and my new path will be like www.SITE_NAME.com/DYNAMIC_PATH


Solution

  • These scripts should be placed in the .htaccess file.

    //*301 Redirect: xyz-site.com to www.xyz-site.com
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www.xyz-site.com$ [NC]
    RewriteRule ^(.*)$ http://www.xyz-site.com/$1 [L,R=301]
    
    //*301 Redirect: www.xyz-site.com to xyz-site.com
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^xyz-site.com$ [NC]
    RewriteRule ^(.*)$ http://xyz-site.com/$1 [L,R=301]
    
    //*301 Redirect: Redirecting Individual pages
    
    Redirect 301 /previous-page.html http://www.xyz-site.com/new-page.html
    

    Or you can use

    Redirect with PHP

    <?php
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: http://www.xyz-site.com" );
    exit(0);
    ?>