A programming & designing blog!

Saturday, November 9, 2013

Redirect to Another URL with PHP

Some times we need to redirect our users to another url or location. In php we can easily redirect user to other link by setting a Location http header using php header().

Simple way to redirect

header('Location: http://google.com');

Though this code is good enough to redirect browser but we need to go through little bit advance way. Because every redirect must have a reason. Why we are redirecting our user to another location. For example, this content is moved or this content is not found etc. if content is moved the valid http response with be HTTP/1.1 301 Moved Permanently or if it not found it will be HTTP/1.1 404 Not Found. It is very helpful to set a valid http response header before redirecting a user especially for SEO. Because web crawler checks http headers before crawl them and trite them by its response. For this purpose it is very important to set a valid response code before redirect. You can use the following php function. It allow you to redirect to another url with specified http response header.


PHP Redirect Function

/**
 * Http redirect
 * 
 * @param string $uri (http://www.w3bees.com)
 * @param int $code (optional) default 302
 */
function redirect($uri, $code = 302)
{
  /* http response codes */
  $http_code = array(
    100 => 'HTTP/1.1 100 Continue',
    101 => 'HTTP/1.1 101 Switching Protocols',
    200 => 'HTTP/1.1 200 OK',
    201 => 'HTTP/1.1 201 Created',
    202 => 'HTTP/1.1 202 Accepted',
    203 => 'HTTP/1.1 203 Non-Authoritative Information',
    204 => 'HTTP/1.1 204 No Content',
    205 => 'HTTP/1.1 205 Reset Content',
    206 => 'HTTP/1.1 206 Partial Content',
    300 => 'HTTP/1.1 300 Multiple Choices',
    301 => 'HTTP/1.1 301 Moved Permanently',
    302 => 'HTTP/1.1 302 Found',
    303 => 'HTTP/1.1 303 See Other',
    304 => 'HTTP/1.1 304 Not Modified',
    305 => 'HTTP/1.1 305 Use Proxy',
    307 => 'HTTP/1.1 307 Temporary Redirect',
    400 => 'HTTP/1.1 400 Bad Request',
    401 => 'HTTP/1.1 401 Unauthorized',
    402 => 'HTTP/1.1 402 Payment Required',
    403 => 'HTTP/1.1 403 Forbidden',
    404 => 'HTTP/1.1 404 Not Found',
    405 => 'HTTP/1.1 405 Method Not Allowed',
    406 => 'HTTP/1.1 406 Not Acceptable',
    407 => 'HTTP/1.1 407 Proxy Authentication Required',
    408 => 'HTTP/1.1 408 Request Time-out',
    409 => 'HTTP/1.1 409 Conflict',
    410 => 'HTTP/1.1 410 Gone',
    411 => 'HTTP/1.1 411 Length Required',
    412 => 'HTTP/1.1 412 Precondition Failed',
    413 => 'HTTP/1.1 413 Request Entity Too Large',
    414 => 'HTTP/1.1 414 Request-URI Too Large',
    415 => 'HTTP/1.1 415 Unsupported Media Type',
    416 => 'HTTP/1.1 416 Requested range not satisfiable',
    417 => 'HTTP/1.1 417 Expectation Failed',
    500 => 'HTTP/1.1 500 Internal Server Error',
    501 => 'HTTP/1.1 501 Not Implemented',
    502 => 'HTTP/1.1 502 Bad Gateway',
    503 => 'HTTP/1.1 503 Service Unavailable',
    504 => 'HTTP/1.1 504 Gateway Time-out'
  );
  
  /* set http response code */
  if (array_key_exists($code, $http_code)) {
    header($http_code[$code], false, $code);
  }

  /* redirect to url */
  header('Location: '.$uri);

  return;
}

Usages

Simple usage

redirect('http://google.com');
exit;

With response code

redirect('http://google.com', 301);
exit;

56 comments:


  1. - Két két két.

    Ba thanh âm thanh thúy truyền ra, cự trảo cũng không tồn tại dưới sự công kích của Ám Ảnh Kiếm được bao nhiêu.

    Lúc này sắc mặt của Địa Ma Lão Nhân trở nên tái nhợt, đối với sự vây hãm của ba người, Địa Ma Lão Nhân hoàn toàn không có lực phản kích.

    - Xoẹt xoẹt.

    Chín sợi râu màu đen đã kéo dài ra, trên đó có một khí tức vô cùng khủng bố.
    dongtam
    mu moi ra hom nay
    tim phong tro
    http://nhatroso.com/
    nhạc sàn
    tổng đài tư vấn luật
    văn phòng luật hà nội
    tổng đài tư vấn luật
    thành lập công ty trọn gói
    http://we-cooking.com/
    chém gió
    trung tâm tiếng anh
    Chín sợi râu tràn ngập đột nhiên bao phủ quanh người của Địa Ma Lão Nhân, chin sợi râu này giống như là chín con cự mãng không ngừng vặn vẹo.

    Sắc mặt của Địa Ma Lão Nhân trầm xuống, quanh thân của hắn ngưng tụ một hắc mang mà phản kích về phía Đại Hắc.

    - Xoẹt xoẹt.

    Chỉ thấy chín rễ chính trên người Đại Hắc không ngăn cản được hắc mang tràn ngập kia, thân hình của Địa Ma Lão Nhân càng ngày càng tới gần.

    ReplyDelete
  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

    SAP training in Chennai

    ReplyDelete
  3. Because web crawler checks http headers before crawl them and trite them by its response. For this purpose it is very important to set a valid response code before redirect. You can use the following php function. It allow you to redirect to another url with specified http response header.
    clicker heroes games, play geometry dash and happy wheels game online!

    ReplyDelete
  4. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.php training in chennai

    ReplyDelete
  5. It is a very nice article including a lot of viral content. I am going to share it on social media. Get the Cold Pressed Oil in online.

    ReplyDelete
  6. Thanks for share .
    Bán thuốc diệt kiến Nhật Bản Super Arinosu Koroki siêu an toàn, diệt 1 con lây chết cả tổ LH 0983131528

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. php training in chennai

    ReplyDelete

  9. Bán Set 7 quan lot GU Uniqlo Nhật Bản chất liệu 100% Cotton hàng hiệu cao cấp giá rẻ call 0983131528

    ReplyDelete
  10. It is easy to understand, detailed and meticulous! I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
    wings io

    ReplyDelete
  11. I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
    vex 3

    ReplyDelete
  12. I strongly believe that there will be great opportunities for those who looked into your site. Please do keep updating us...
    Best Online Software Training Institute | PHP Training

    ReplyDelete
  13. Though this code is good enough to redirect browser but we need to go through little bit advance way. Because every redirect must have a reason. Why we are redirecting our user to another location. For example, this content is moved or this content is not found etc. if content is moved the valid http response with be HTTP/1.1 301 Moved Permanently or if it not found it will be HTTP/1.1 404 Not Found. It is very helpful to set a valid http response header before redirecting a user especially for SEO. Because web crawler checks http headers before crawl them and trite them by its response. For this purpose it is very important to set a valid response code before redirect. You can use hotmail.com and the following php function. It allow you to redirect to another url with specified http response header.

    ReplyDelete
  14. What happened to Hotmail? First launched in 1996 the world's first free web-based email service, www.hotmail.com was later renamed to Outlook.com.

    ReplyDelete
  15. Very useful content you have shared. Really great. Thank you.

    PHP Training in Chennai
    PHP Course in Chennai

    ReplyDelete
  16. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! windows 7 professional product key

    ReplyDelete
  17. Really I enjoy your blog with an effective and useful information. Very nice post with loads of information. Thanks for sharing with us.
    PHP Training Chennai
    PHP Training Institute in Chennai

    ReplyDelete
  18. Your website is worth to visit all of your posted content which was very interesting to read. Thanks a lot to share information like this.
    Salesforce Training institutes in Chennai
    Salesforce Course in Chennai

    ReplyDelete
  19. Really I enjoy your blog with an effective and useful information. Very nice post with loads of information. Thanks for sharing with us. net worth

    ReplyDelete
  20. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it…
    Best cloud Computing Training Institute in Chennai | Best cloud Computing Training Institute in Velachery

    ReplyDelete
  21. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and I will surely implement them in my study.
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

    ReplyDelete
  22. This is definitely worth a while. Thanks for this post!

    Lorisa

    ReplyDelete
  23. Nice blog with excellent information. Thank you, keep sharing

    ReplyDelete
  24. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.



    AWS Training in Bangalore | Amazon Web Services Training in Bangalore


    AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli


    AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

    ReplyDelete
  25. Thanks for providing such a great information in the blog and also very helpful to all.
    DevOps Online Training

    ReplyDelete
  26. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    python course in pune
    python course in chennai
    python course in Bangalore

    ReplyDelete
  27. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    excel advanced excel training in bangalore | Devops Training in Chennai

    ReplyDelete
  28. Thanks for sharing this information admin, it helps me to learn new things

    mbaguide
    Guest posting sites

    ReplyDelete
  29. DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.


    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

    ReplyDelete
  30. So nice and interesting items! Lovely selection darling! Nice post.
    gmail account

    ReplyDelete
  31. Amazing post!!! This post is very comprehensive and i learn more ideas. Thanks for your post, i like more updates from your blog...
    PHP Training in Bangalore
    PHP Institutes in Bangalore
    PHP Course in Chennai
    PHP Course in Annanagar
    PHP Course in Aminjikarai
    PHP Course in Sholinganallur

    ReplyDelete