Freedom, Community & Sustainability

HTML, PHP or htaccess: Redirect or Refresh after delay

January 2, 2011 -- William
Last modified on July 2014
Duration: 5 minutes or less

Redirecting and refreshing are almost the same thing, actually, refreshing is just a redirection to the same page (loading it once again). When you do such redirections you can also inform about what it is about: is is permanent? is it temporary? is there just nothing to worry about? These extra information is very important for SEO purposes. They can explain to search engines why this redirection is happening and what action should be taken. So, before doing the redirections, let's talk a little bit about HTML status codes.

Redirection types:
The HTML status codes that start with 3xx inform that a redirection is taking place. The most important one is the 301, the page moved permanently to another URL, meaning that search engines will update their links to the new URL and transfer all information from the previous URL to the new one. The second most important is the 302 meaning that the page moved temporarily, the new page was found and no other action is necessary. On a 301 redirect Google will transfer the PageRank of the page to the redirected page, I imagine other search engines do something similar. This is nice when you change the address of your site for example. Now let's do our redirections.

Different techniques to redirect:
There are many techniques to redirect, we will see here three different ones: the old school way with an HTML meta tag, the server way through a PHP script and the Webmaster way with the .htaccess file. Let's take a look at them.

Redirecting with HTML meta tag (legacy)

Insert this tag into the header of your HTML page between <head> and </head>.

<meta content="5;url=http://www.example.com" http-equiv="Refresh" />

The number after "content" is the number of seconds before the page is redirected towards the address on "url". If the "content" is set to 0, Google will consider it is a 301 redirect, otherwise it will be 302. If the URL points to the same page then it will refresh it's content. This is a good way to refresh your content because you have control over the delay during which a page will be displayed to the user.

Redirecting with PHP

Put this script at the beginning of the page, it might as well be the only thing on the page because the redirection will be processed directly by the server and the page will not be shown to the user.

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

PHP does not offer a way to have a delay (since the redirection happens on the server) so you should never use this to refresh the page. If this script points to the same page it will create an infinite loop.

Redirecting with .htaccess

Place this code in the .htaccess file and put the file in the root folder of your site. You can chose to put the status code or omit it for a 302 redirection.

Redirect 301 /old-page.html http://www.example.com/new-page.php

You can also redirect entire folders with just one line.

Redirect 301 /old-folder/ http://www.example.com/new-folder/

Or entire sites.

Redirect 301 / http://www.new-site.com/

If you need a little bit more control over your redirection, you can take advantage of regular expressions and write something like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old-site.com$ [NC]
RewriteRule ^.*$ http://new-site.net/$1 [R=301,L]

The "RewriteCond" is the condition for a URL rewrite. A redirection will only take place if the condition is met. The [NC] means "No Case", not case sensitive. Right before the URL you can choose which URL should be redirected:

"^" = "request for site starting with..."

"!^"= "request is not for site starting with..."

The "RewriteRule" tells how the new address will be built. The ending "$1" is to be used if your new site has exactly the same structure as your old one. In this case the URL www.old-site.com/page1.html will redirect to www.new-site.net/page1.html. If you want people to be redirected to the front page of the new site no matter what page they ask from the old site then just omit the "$1".

At the very end [R=301, L] means that it is a 301 redirection and that this is the Last command (no other rule will be considered for this condition).

This is the usual method to redirect from http://www.url.tld to http://url.tld (or the other way around). Tweak the code to say "if my site is not url.tld$ then redirect to url.tld$1" if you see what I mean.

Well, that's it! You probably already know what is the technique and the status code you need but just to recapitulate:

301 = permanent redirection that will update search engine links and transfer your Google PageRank.
302 = undefined, temporary redirection, the page is considered to be found.

HTML meta tag = let you set a delay.
PHP script = redirection made on the server.
htaccess = redirection of folders and entire sites possible, using conditions and regular expressions.

Thank you for reading this tutorial!

Add new comment