AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

How to Redirect a Web Page

By pushpam abhishek
Listen to this article
How to Redirect a Web Page

How to Redirect a Web Page


This post describes the way to properly redirect the web page using an HTTP 301 status code and site header. The 301 status code is employed to point that a page has permanently moved. 301 redirect is the most effective and program Friendly method for webpage redirection. PHP redirect tells the browser (or an inquiry engine bot) that the page has been permanently moved to a replacement location.




<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.new-domain.com/");
exit();
?>

If you set the Location header by itself, PHP automatically sets the status code to HTTP/1.1 302 Found.

Note: if you attempt to send headers after content has been sent, you will get a warning like, "Warning: Cannot modify header information - headers already sent by ...". Look for empty lines and spaces between PHP open and close tags.

Tip: Use a lower-case name for the header function (not Header) to make sure your PHP redirect code is compatible with PHP 6.

You can redirect the page to a new location along with parameters; this PHP code will redirect users to a new location along with its query string:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.new-domain.com".strtolower($REQUEST_URI));
exit();
?>

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments