PHP Permanent Redirect

  • follow us in feedly
Published May 14, 2013 by Brad Knutson
PHP Permanent Redirect

There comes a time when every webmaster must redirect one page to another. Perhaps the page has moved, or possibly the domain has changed. Whatever the reason, we need a way to redirect the old page to the new page. This redirect must be behind the scenes, and SEO friendly.

Ideally, this would be done by the web server software (Apache, for example), but there are legitimate situations where this needs to be done by a server-side scripting language. You could also write a redirect in Javascript or jQuery, but this isn’t SEO friendly at all. Stick with the server-side redirect.

One final note before we dig into some code. The redirect needs to be permanent (a 301 redirect), not temporary (a 302 redirect). Google says:

If you need to change the URL of a page as it is shown in search engine results, we recommended that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.

Redirect One Page to Another

	header("HTTP/1.1 301 Moved Permanently");


<?php header("Location:", true, 301); ?>

Redirect from file to directory root

What if you want to redirect from a file, for example, default.php to the directory root? We can do that too!

if(isset($_SERVER['HTTPS']) and 'on' === $_SERVER['HTTPS']) {
    $location = 'https://';
} else {
    $location = 'http://';
// Get the servers base URL
$location .= $_SERVER['SERVER_NAME'] . '/';
// Get the current URI without a file name in it
$location .= dirname($_SERVER['REQUEST_URI']) . '/';
header('Location: ' . $location);

I took out the PHP tags so don’t forget to put those in if you need to.

That’s essentially it. If you need a more broader scale redirection (like an entire domain or subdomain), or need a more programmatic redirection, then you should probably consider writing your redirect in .htaccess

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date



See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

One thought on “PHP Permanent Redirect

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">