How to make a redyrect PHP

"Learn how to use PHP redirects with this step-by-step example; make your site easier to navigate & improve SEO."

Redirecting in PHP

A redirect is a way to send both users and search engines to a different URL from the one they originally requested. Redirects are used for various purposes, such as when a website moves to a new address or when a user requests a certain page that no longer exists. Redirects can be accomplished in PHP using the built-in header() function.

The header() function must be called before any other output is sent to the browser. This means that it should be the first thing that is called in the PHP script. The function takes one argument, which is the header string to be sent. To redirect a user or search engine to a different URL, the Location header should be used. This is done by setting the argument to 'Location: http://www.example.com', where http://www.example.com is the URL to which the redirection should take place. In order to make the redirection permanent, the 301 status code should be set in the header string. This is done by adding the 301 status code to the beginning of the header string, like so: 'HTTP/1.1 301 Moved Permanently'. The full header string should look like this:

header('HTTP/1.1 301 Moved Permanently Location: http://www.example.com');

This will redirect the user or search engine to the URL http://www.example.com. If the redirection should only be temporary, then the 302 status code should be used instead. It should be noted that the header() function must be called for each redirect. This means that if a user is redirected to a different page and then that page should be redirected to another page, then the header() function must be called twice.

Redirecting in PHP is a simple process that can be accomplished using the built-in header() function. The header() function must be called before any other output is sent to the browser, and the argument should be set to the Location header and the desired URL. In order to make the redirection permanent, the 301 status code should be used, while the 302 status code should be used for temporary redirects. The header() function must be called for each redirect.

Answers (0)