Redirect 301 in PHP and .htaccess

Redirect 301 in PHP and .htaccess

What is 301 redirect ?

You have a website and its all pages are ranked in the search engine. And now you’ve moved these files to a new domain. What will happen in this scenario!!. You are sending the visitor to “Error 404 – File not found” page when they follow your website in the search engine. Furthermore, if you place the custom error page, then also you will be keep loosing ranking in upcoming days in search engines as the original file could not be found in the old URL. So what is the solution for this?? The solution is 301 redirect which means moved permanently. It is the most efficient and search engine as well as visitor friendly method for the webpage redirection.


Why do you need 301 redirect ?

Have you ever typed yahoo.com in your browser, what happens next is it will redirect you to www.yahoo.com, its all the result of the 301 redirect. so, why do you need to do so?. If you’re not doing so, then you’re splitting yourself to two different URL and search engine spiders can also crawls your site’s content by two different URL which may result to “duplicate content” penalty by search engines if search engines collect two copies of the same data from two different URLs.

How to do 301 redirect in PHP ?

If you want to redirect a single page to another location then put the following code in the old page which have been moved.

<?php 
 header('HTTP/1.1 301 Moved Permanently');
 header('Location: http://www.new-domain.com/new-file-name.php'); 
?>


If you want to redirect non-www to www domain name then put the following code into top of every page of the website

<?php 
 if (substr(getenv('HTTP_HOST'),0,3) != 'www') { 
 header('HTTP/1.1 301 Moved Permanently');
  header('Location:http://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 
 } 
?>

How to do 301 redirect in .htaccess file?

Put the following code into the .htaccess file and upload it into the root folder of the website. Note that this file only works for the website which is running under Apache Web Server.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Related Posts


49 thoughts on “Redirect 301 in PHP and .htaccess

  1. selbstkühlende eismaschine

    I like this web blog very much, Its a really nice place to read and get info .

    Reply
    1. Nihal

      you can call me e Posted on The Simple TruthTruth SerumTruth or ConsequencesHonesty or BustIt actually soudns like an interesting blog. I wouldn’t mind checking it out when it is up and running!

      Reply
  2. Lauren Tave

    I simply want to tell you that I’m new to blogging and honestly enjoyed your web-site. More than likely I’m planning to bookmark your blog . You certainly have beneficial stories. With thanks for sharing with us your web site.

    Reply
  3. Denita Marreel

    This is really interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your excellent post. Also, I’ve shared your site in my social networks!

    Reply
  4. heatingoilin Englandin Walesin Scotland

    Hello there, You’ve done a great job. I’ll definitely digg it and for my part recommend to my friends. I am sure they will be benefited from this web site.

    Reply
  5. quotesEngland

    Excellent site. Lots of useful information here. I am sending it to several buddies ans also sharing in delicious. And certainly, thanks for your sweat!

    Reply
  6. horsefacts

    Hey There. I found your weblog using msn. This is a really well written article. I will make sure to bookmark it and come back to learn more of your useful information. Thanks for the post. I’ll certainly comeback.

    Reply
  7. First real investment online!

    Hi my family member! I wish to say that this post is amazing, great written and include almost all important infos. I would like to see extra posts like this .

    Reply
  8. Try first real and legit online investing company and invest your money in real buisnesses and get paid every month

    First real investment online! Just buy share(s) of real European projects and get paid every month. Registred company with all certificates. No HYIP!

    Reply
  9. vitamix|

    I have learn a few just right stuff here. Definitely value bookmarking for revisiting. I surprise how much effort you put to create any such wonderful informative web site.

    Reply
  10. computer,hack,exploit

    Thanks for the good writeup. It in truth was once a leisure account it. Look complicated to far brought agreeable from you! By the way, how could we keep up a correspondence?

    Reply
  11. shared web hosting

    I think this is among the most vital information for me. And i am happy studying your article. But want to commentary on some basic things, The site style is wonderful, the articles is actually nice : D. Good task, cheers

    Reply
  12. We know Search Engines - Business SEO Master

    Thank you for another great article. Where else may anyone get that type of information in such a perfect way of writing? I have a presentation next week, and I am at the search for such info.

    Reply
  13. nam lim

    Its like you read my mind! You appear to grasp so much approximately this, like you wrote the ebook in it or something. I believe that you simply could do with some percent to force the message house a bit, but other than that, this is magnificent blog. A great read. I’ll certainly be back.

    Reply
  14. adult

    Hello There. I discovered your weblog the use of msn. That is an extremely well written article. I’ll be sure to bookmark it and come back to read extra of your helpful info. Thanks for the post. I’ll definitely comeback.

    Reply
  15. house and lot quezon city

    Useful info. Fortunate me I found your site unintentionally, and I am stunned why this coincidence didn’t happened in advance! I bookmarked it.

    Reply
  16. elektro zigarette kaufen

    You are actually a excellent webmaster. The website loading pace is incredible. It kind of feels that you’re doing any distinctive trick. In addition, The contents are masterwork. you have performed a wonderful process on this subject!

    Reply
  17. salud al dia

    Usually I don’t learn post on blogs, however I would like to say that this write-up very pressured me to take a look at and do it! Your writing taste has been surprised me. Thanks, very great post.

    Reply
  18. what causes strokes

    Thanks for the auspicious writeup. It actually used to be a entertainment account it. Glance complicated to more added agreeable from you! However, how could we be in contact?

    Reply
  19. salben gegen schuppenflechte

    You really make it appear so easy together with your presentation but I to find this topic to be actually something that I think I might by no means understand. It sort of feels too complicated and very huge for me. I am looking ahead on your next post, I will try to get the grasp of it!

    Reply
  20. impresas de hormigon

    Heya i’m for the primary time here. I came across this board and I find It truly useful & it helped me out much. I’m hoping to offer something again and help others like you aided me.

    Reply
  21. Daniela

    I’m speechless. This can be a speurb blog and very attractive too. Nice work! That’s now not actually a lot coming from an newbie publisher like me, however it’s all I may just say after diving into your posts. Nice grammar and vocabulary. Not like other blogs. You actually recognise what you?re talking about too. Such a lot that you made me want to discover more. Your blog has develop into a stepping stone for me, my friend.

    Reply
  22. real estate philippines

    My brother suggested I would possibly like this blog. He used to be totally right. This submit actually made my day. You can not consider simply how a lot time I had spent for this info! Thanks!

    Reply
  23. philippine real estate

    I don’t even understand how I finished up here, but I thought this post was once great. I do not recognise who you might be but definitely you are going to a famous blogger when you are not already. Cheers!

    Reply
  24. Stormy Zins

    Recently, I didn’t give a great deal of consideration to leaving suggestions on weblog web page posts and have placed remarks even considerably less.

    Reply
  25. Elmer Rafla

    I just want to tell you that I’m very new to blogging and seriously enjoyed you’re website. Likely I’m likely to bookmark your site . You actually have very good stories. Thanks a bunch for revealing your web-site.

    Reply
  26. Athena

    Greetings thanks for wonderful submit i used to be searching for this concern very last two days. I’ll look for future precious posts. Have enjoyable admin.

    Reply
  27. Shaunta Jae

    Thanks for your your content – really interesting. We love our Kindle and the best site for tips and tricks is coolkindles.

    Reply
  28. maikäfer

    You actually make it seem so easy together with your presentation however I find this topic to be actually something which I think I would by no means understand. It kind of feels too complicated and extremely wide for me. I’m looking ahead to your subsequent submit, I’ll try to get the hang of it!

    Reply
  29. Magdalen Morris

    Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!

    Reply
  30. Paulette Gunkelman

    I needed social media icons for new website and this site helpes me a lot for finding these icons. Many thanks a lot…

    Reply
  31. Lorenza Vanhese

    By the way.Is your theme custom made or did you download it from somewhere?A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Thanks

    Reply
  32. Barbera Callado

    I just want to mention I’m newbie to blogs and definitely enjoyed this blog. Likely I’m planning to bookmark your blog post . You certainly come with very good articles. Appreciate it for sharing with us your blog.

    Reply
  33. bigcat

    I simply wished to appreciate you once again. I’m not certain what I might have followed in the absence of the actual methods contributed by you relating to my situation. It absolutely was the scary scenario in my opinion, however , encountering a expert avenue you treated it forced me to leap over contentment. I am happy for the assistance and in addition pray you comprehend what a powerful job you happen to be undertaking teaching most people by way of your websites. Probably you haven’t encountered all of us.

    Reply
  34. Dewey Schickel

    Nice read, I just passed this onto a friend who was doing some research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thanks for lunch!

    Reply
  35. sun residences

    I take pleasure in, cause I found just what I used to be looking for. You have ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

    Reply
  36. How to get into Harvard College

    Wonderful website. A lot of useful information here. I’m sending it to several friends ans also sharing in delicious. And naturally, thanks for your effort!

    Reply
  37. content

    Hello there, simply turned into aware of your blog thru Google, and found that it is really informative. I’m gonna watch out for brussels. I will appreciate if you continue this in future. A lot of other folks will be benefited from your writing. Cheers!

    Reply
  38. tops sites worth

    Hello there, I found your website by means of Google whilst looking for a similar matter, your site came up, it appears to be like great. I’ve added to my favourites|added to my bookmarks.

    Reply
  39. hormigon impreso precios

    Wonderful things right here. We are thrilled to visit your write-up. Thank you so much considerably and i’m taking a look forward to feel an individual. Are you going to be sure to decrease me a e-mail?

    Reply
  40. bloques de hormigon

    It’s just a great plus very helpful part of information and facts. I’m just fulfilled that you simply provided this handy information and facts with us. Remember to keep us updated in this way. Appreciation for sharing.

    Reply
  41. piedra para fachada

    hey there! , Everyone loves ones publishing thus lots! percent we all communicate added approximately this page for America online? My spouse and i involve an experienced during this place to resolve this difficulty. May be that is certainly you! Waiting for watch you.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*