Permanent (301) Redirects with PHP

If you want to permanently redirect a small number of URIs, or redirect based on conditions defined in PHP, then you need a permanent PHP redirect.

To redirect clients based on things like screen resolution or window size, you should use javascript redirects.

For most situations where you want to send visitors elsewhere, a server side (HTTP) redirect is usually the best choice. The use of mod_alias and mod_rewrite (on Unix servers), perhaps within a htaccess file, are often the best methods of accomplishing server-side redirects. You can`t beat mod_rewrite for flexibility.

Unfortunately, not everyone has access to these modules, and in any case you may need to do some PHP processing before performing the redirect.

A basic permanent (301) redirect in PHP uses the code below:

<?php
// This header tells the client the HTTP status code of the page
header("HTTP/1.1 301 Moved Permanently");
// The client must then be informed where the URI is now located
header("Location: http://www.example.com/example");
exit();
?>

If the HTTP status code is not specified, then the redirect will automatically result in a 302 code (saying the the page has been ‘found’ or in other words has only moved temporarily). Unless you want clients (and search engines) to continue using the previous URI, then you need to specify the status.

Kviečiu įvertinti įrašą!
[Viso: 0 Vidurkis: 0]

Similar Posts

Parašykite komentarą

El. pašto adresas nebus skelbiamas.

99 − = 89