Skip to main content

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;

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. This is really too useful and have more ideas from yours. keep sharing many things and thanks for sharing the information.

    Best python training institute in Kanchipuram

    ReplyDelete

  13. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...

    Best Java Training Academy in Kanchipuram

    ReplyDelete
  14. thanks for sharing this article
    I Found this article useful same as your. you can visit by clicking java training in Hyderabad

    ReplyDelete
  15. I have read your blog its very attractive and impressive. I like it your blog.
    Best Software Testing Training Academy in Kanchipuram

    ReplyDelete
  16. thanks for sharing this article.....

    ReplyDelete
  17. kita harus bisa lebih jeli dan lebih penting untuk memperhatikan semuanya. Jika kita coba untuk melakukan proses seleksi, maka ini akan memudahkan siapa saja untuk bisa lebih berhasil lagi kemudian.
    asikqq
    pionpoker
    dewaqq
    bandar ceme
    sumoqq
    hobiqq
    paito warna
    interqq
    forum prediksi

    ReplyDelete
  18. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Best web Designing Training Training Institute in Kanchipuram

    ReplyDelete
  19. I am looking for and I love to post a comment Python training in punethat "The content of your post is awesome" Great work!

    ReplyDelete
  20. nice article
    Unicsol offers Best full stack developer course in hyderabadget trained by 10+years of experienced faculty and get placed as full stack developer.

    ReplyDelete
  21. คาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย

    ReplyDelete



  22. flipkart debit card offers,
    flipkart debit card emi,
    debit card offers in flipkart,
    flipkart sbi debit card offers,
    sbi debit card offers on flipkart,,
    flipkart debit card emi axis,,
    flipkart debit card emi sms number
    flipkart offers for hdfc debit card,
    hdfc debit card offers on flipkart
    flipkart icici debit card emi offer,,
    flipkart debit card emi message,,
    kotak debit card offers on flipkart
    flipkart icici debit card offer,,,
    icici debit card offers on flipkart
    flipkart debit card emi kotak,
    flipkart sbi debit card emi offer,
    flipkart hdfc debit card emi offer,
    flipkart debit card emi sbi sms,,
    flipkart debit card emi hdfc eligibility
    flipkart axis bank debit card offers
    flipkart debit card shopping
    flipkart debit card loan,
    flipkart debit card no cost emi,
    flipkart debit card option,
    flipkart debit card purchase,
    flipkart debit card offers on mobiles,
    flipkart debit card mobile
    flipkart kotak debit card offer,
    flipkart debit card scheme,,
    flipkart debit card payment offers,
    idbi debit card offers on flipkart,,
    sbi debit card offers in flipkart,,
    flipkart debit card installment,
    flipkart sbi debit card offer today,,
    flipkart bank card offers,,
    flipkart debit card discount
    flipkart axis debit card offer,
    yes bank debit card offers on flipkart,
    flipkart offers for sbi debit card
    flipkart big billion day debit card offers,
    flipkart debit card ,
    flipkart debit card emi offer details,
    flipkart offers for icici debit card,,
    flipkart pnb debit card offer,
    flipkart sbi debit card offer terms and conditions,
    hdfc debit card offers in flipkart,
    axis bank debit card offers in flipkart

    ReplyDelete


  23. Thanks for sharing such a good article. This will help New Bloggers Like me


    flipkart Credit card offers,flipkart offers,flipkart offer
    flipkart emi,flipkart debit card emi,flipkart emi on debit card,,flipkart with emi,flipkart emi debit card sbi,flipkart emi sbi debit card,
    flipkart sbi,flipkart sbi debit card emi,flipkart sbi offer


    flipkart hdfc,flipkart hdfc offer,flipkart hdfc credit card offer,flipkart hdfc bank offer,flipkart icici debit card emi,flipkart icici credit card offer,flipkart icici credit card offers,flipkart offer for icici credit card,flipkart icici offer,flipkart icici debit card offer,flipkart debit,flipkart debit card emi,flipkart emi for debit card,flipkart sbi debit card emi,flipkart debit emi,flipkart icici debit card emi,flipkart debit card offers,flipkart offer debit cardflipkart debit card emi eligibility smsflipkart debit card emi icici,flipkart sbi debit card offer,flipkart hdfc debit card emi,flipkart credit,flipkart credit card offers,flipkart offer credit card,flipkart offer for credit card,flipkart offers with credit cards,flipkart hdfc credit card offer,flipkart credit card,flipkart offer,flipkart sale,

    Flipkart SBI Offer 2019: 10% Instant Cashback.
    Flipkart HDFC Offer 2019: 10% Instant Offer.
    Flipkart Cashback offer 2019: 10% Cashback.
    Flipkart SBI Offer 2019: 10% Instant Cashback.
    Flipkart HDFC Offer 2019: 10% Instant Offer.
    Flipkart Cashback offer 2019: 10% Cashback.
    SBI Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashbackhttps://www.flipkart.com/?affid=crishkuma1
    HDFC Credit Ca rd /Debit Card Offers 10% Instant Cashback
    Get Cashbackhttps://www.flipkart.com/?affid=crishkuma1
    Citi Bank Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashbackhttps://www.flipkart.com/?affid=crishkuma1
    Axis Bank Credit Card /Debit Card Offers 10% Instant Cashback (Extra 5% on Axis Buzz Cards)
    Get Cashback
    https://www.flipkart.com/?affid=crishkuma1
    ICICI Bank Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashback
    https://www.flipkart.com/?affid=crishkuma1
    YES Bank Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashback
    https://www.flipkart.com/?affid=crishkuma1
    IDBI Bank Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashback
    https://www.flipkart.com/?affid=crishkuma1
    Kotak Bank Credit Card /Debit Card Offers 10% Instant Cashback
    Get Cashback
    https://www.flipkart.com/?affid=crishkuma1
    flipkart sbi debit card offers,flipkart offers


    flipkart offers


    flipkart axis offers

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

    ReplyDelete
  25. I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Python Training in pune experts who know advanced concepts which can help to solve any type of Real-time issues in the field of Python. Really worth trying Freelance seo expert in bangalore

    ReplyDelete
  26. Attend The Analytics Course in Bangalore From ExcelR. Practical Analytics Course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Analytics Course in Bangalore.
    ExcelR Analytics Course in Bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery. Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar.We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script.
In this post I am going to share a simple but useful force file downloader script.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin. It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.