SEO.fr Agency > Blog > Redirects and URL rewriting

Redirects and URL rewriting

The automatic switching of one page to another is called “redirection”. There are many techniques for achieving this result, and they can be used in a variety of different cases.

Use of redirects

There are several reasons why you might want to use a :

Change page names or addresses

  • You want to change the name or address of one of your pages without losing the existing links to the old page name/address. You will therefore organise a redirection from the old address to the new one. Internet users who enter, for example, the old page address in their browser, automatically land on your new page, without necessarily having detected the ploy. In the same way, the “PageRank” from which your old page benefited is transferred to the new one. This problem can affect your entire site if you have changed your domain name.
  • You want to simplify your page names. Some dynamic site management systems produce long URLs that are not very “presentable” such as: “http://www.example.com? theme1&lang=en&user=any&chapter1”. This complexity can be completely hidden from users by using redirects. The process is simply to use a dummy page named “chapter1en.htm” and redirect “chapter1en.htm” to “?theme1&lang=en&user=any&chapter1”. You can then present users with the address “http://www.example.com /chapitre1en.htm” which will work perfectly. However, it should be noted that this is not a redirection as such, but rather what is known as URL rewriting.

Making statistics

  • You want to keep track of the download of certain files. Instead of having your “Download” button point directly to the file, you point it to an intermediate page that redirects the Internet user to the file. This intermediate page will contain a marker that will feed your statistics by reporting the download to your database. This system is very commonly used by sites offering downloads. It results in the display of a window (this is the intermediate window) with a text such as “If the download has not started automatically in 5 seconds, click on this button”.
  • You wish to make statistics concerning your outgoing links. Instead of having your links point directly to the targeted sites, you have them point to intermediate pages that redirect the user to these sites. This intermediate page will contain a marker that will signal the action to your database. This process is used by several directory systems and is known as “soft linking”. However, it has several disadvantages and should always be replaced by the use of “Onclick”.

Being dishonest

  • You want to do a wild redirect, i.e. “steal” the pagerank of a site. Sorry for you, it’s too late! Until recently, redirects made it possible to take advantage of a loophole in the Google system and to recover the PageRank of a page by simply redirecting to it. If a healthy curiosity pushes you to understand the mechanisms of this trick, read these explanations on wild redirects. Above all, remember that this use of the 302 redirect is now severely punished by Google.
  • You want to present different content to search engines and human visitors (cloaking). Some redirections (e.g. JavaScript) are taken into account by browsers, but are not handled by search engine crawlers. This makes it possible to redirect human visitors to a page intended for them, while the robots continue to see only the original page. The German BMW website was penalised by Google for using this technique. You would be taking a huge risk by using it.

Technical principles for redirection

There are two main ways to implement redirections:

At the server level

(using a header in Php, ASP or Cold Fusion or using the .htaccess file).

This is the cleanest and most radical way to proceed. This is the solution you should choose for changes of page names or addresses. It is also used for some types of wild redirects. When you use the .htaccess file, you are asking your server to “translate” certain URLs and therefore transform them before you even try to access the matching pages, which saves time for your visitors. The .htaccess file is also the only satisfactory way of doing “URL rewriting” (simplifying URLs that are too complex, for example).

Redirects performed at the server level are sometimes called “HTTP redirects”. They are invisible to the uninformed Internet user, but it is good to know that the server nevertheless signals to the browser (or robot) that a redirection has taken place. Just before sending the page targeted by the redirect, the server sends a code indicating what kind of redirect it is.

At the level of the Internet user’s browser

(“Meta Refresh” method or Javascript/Flash/DHTML redirection, etc.)

A first page is loaded in the visitor’s browser, then after a variable time, the visitor is forced to switch to the final page or to the file to be downloaded. This solution can be interesting for managing your download statistics. However, if the target of the redirection is a URL, this method has serious disadvantages:

  • The engines are unable to follow redirects programmed in Javascript or Flash. It is therefore possible that Google interprets the situation as a kind of cloaking.
  • This type of “soft” link penalises the targeted sites insofar as they do not get the “PageRank” that your links should bring them. They also penalise your own site, which no longer benefits from the relevance provided by outgoing links.
  • The browser’s “Back” button becomes inoperative, which can confuse novice users and annoy others.
  • Some browsers do not execute the “Meta Refresh” instruction and some users disable the execution of JavaScript code. Flash code is not executable on all machines. The results of this type of indirection are therefore uncertain. For all these reasons, we strongly advise you to use the “OnClick” mentioned above, rather than a simple redirection.

Some wild redirects used to be done using the “Meta Refresh” method but this no longer works.

The different types of redirection

The .htaccess file, Php, Asp and Cold Fusion make it possible to specify to browsers or robots visiting the page the reason why the redirection has been set up.

While these nuances are of little importance to browsers (and therefore to Internet users), they are very important to robots. Here is what they correspond to:

301 Moved Permanently: Document moved permanently

The original address must be completely replaced by the target address. The page retains its PageRank and its place in the search engine results (unless its content has changed).

302 Found: Document moved temporarily

The original address must be completely replaced by the target address. The page retains its PageRank and its place in the search engine results (unless its content has changed).

303 See Other: Document temporarily replaced by another

The original page must be replaced by the target page, but the original page must remain in the index. This code is rarely used.

307 Temporary Redirect: Document moved temporarily

The above definitions are theoretical. In practice, code 302 is understood as “Document temporarily moved or replaced” and code 303 is not used.

For more details, see Status Code Definitions from W3.org

Got an SEO question?
Julia can help

8 years of SEO expertise

Example of redirection

Enough talk, let’s see how it works in practice:

Using the .HTACCESS file

To create an .htaccess file, simply start by creating a .txt file in which you will enter the instructions for the redirection. For example :

RedirectPermanent oldpage.html http://www.example.com/nouvellepage.html
Note that the new address is in all cases absolute (the address must be integer).
Once your file is saved, you will have to rename it “.htaccess” and copy it in the folder containing “oldpage.htm” (or in the root of your site if oldpage.htm is in the root of your site).
Please note the following points:
The transfer of your file to your FTP must be done in ASCII mode.
If your .htaccess file contains an error, all the pages in the folder in which you have copied it will become inaccessible (you will get a 500 error each time). Delete this file with your FTP client and try again.
Not all servers allow the use of the .htaccess file. To be able to benefit from its functionalities, your server must be an Apache server (which is generally the case) running under Unix or Linux (which is generally the case) and your access provider must not have deactivated the “mod_Alias” module (which is, unfortunately, the case with several free access providers).
To move an entire site, you can use a syntax such as
RedirectPermanent / http://www.nouveaudomaine.com/

Orders and meaning :

RedirectPermanent or Redirect Permanent or Redirect 301: 301 redirect
Redirect or RedirectTemp or Redirect temporary or Redirect 302 : 302 redirect
Redirect Seeother or Redirect 303 : redirect 303
Redirect Gone or Redirect 410 : error 410 (the page no longer exists and has no replacement)
You can use Redirect with other numeric arguments, for example :
Redirect 307 for a 307 redirect

The “.htaccess” file also allows you to manage what is called “URL rewriting”. This is only possible if the “mod_rewrite” module of your Apache server is activated (this module is one of the components of Apache, just like “mod-Alias” or Php, for example).

Example :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^oldpage.html$ newpage.php [L]
With this syntax, the visitor or the robot will be redirected to newpage.htm in a completely transparent way. No redirection code will be sent and the robots will not take any special steps to update their indexes. If you want to signal the redirection to obtain the same result as with the “RedirectPermanent” command, you can replace the last line with :
RewriteRule ^oldpage.html$ newpage.php [R=301,L]

Use of an HMTL header

The redirection is managed by META tag http-equiv=”Refresh”. It instructs the browser to redirect the user to a specified URL after a certain number of seconds. This code must be placed in the page located at the old address:
This page has been moved to the following address: http://www.example.com/nouvellepage.htm
The number 0 can be replaced by the number of seconds to wait before the redirection (the page with the redirection remains displayed during the wait).
If this number is greater than or equal to 3, Google will consider that it is dealing with 2 very distinct pages and will index the page with the redirect and the target page separately.
If this number is less than 3, Google will consider that the page containing the redirect does not deserve to be indexed and will treat the redirect in the same way as a 302 redirect (with the corresponding risks of penalties!)
But this interpretation is specific to Google and is not necessarily shared by the robots of other engines. Unfortunately, it is not possible to explicitly specify the type of redirect desired when using this method.
It should also be pointed out that some browsers do not interpret the META Refresh tag and that this redirection will therefore not always work.

Using a PHP header

The following code should be placed at the top of the page at the old address:

<?php
/* Redirect to a page in the same folder as the current page */function redirect($page) {
$host = $_SERVER[‘HTTP_HOST’]; /* We get the name of the site */
$uri = rtrim(dirname($_SERVER[‘PHP_SELF’]), ‘/\\’); /* We get the name of the folder */
$url = ‘http://’.$host.$uri.’/’.$page; /* We add the name of the target page */
header( « Status: 301 Moved Permanently », false, 301); /*the second parameter 301 is required for some servers */
header(‘Location: ‘.$url);
echo ‘<a href= »‘.$url.’ »>Click here to be redirected to‘.$url.'</a>’;
exit;
}
redirect(‘nouvellepage.php’);
?>
Warning: This code must be placed at the top of the page. A simple space or carriage return placed before the “<?php” will result in an error.
The echo line allows you to manage browsers that do not support http redirections. For more details, read Php redirects. And here is a much simpler example of a Php redirect:
<?php
header(« Location: http://www.example.com/nouvellepage.htm »);
?>

Use of ASP code

The following code should be placed at the top of the page at the old address. The result is the same as with a Php code

<%@ Language=VBScript %>
r> <% response.status = « 301 moved permanently » response.addheader « location », « http://www.example.com/nouvellepage.htm » response.end %>

Using a Cold Fusion code

The following code should be placed at the top of the page at the old address. The result is the same as with a Php code

<CFHEADER statuscode= »301″ statustext= »Moved Permanently »>
<CFHEADER name= »Location » value= »http://www.example.com/nouvellepage.htm »>

Use of a Javascript code

As mentioned above, this method is strongly discouraged. However, here is an example. The following code should be placed in the page at the old address:

<html> <head>
<script language= »javascript » type= »text/javascript »>
<!– window.location.replace( « http://www.example.com/nouvellepage.htm »); –>
</script> </head> <body>
This page has been moved to the following address:
http://www.example.com/nouvellepage.htm
</body> </html>
This method will not work for Internet users who have disabled Javascript execution. Remember that it will not be taken into account by the robots. The page containing the redirection will therefore remain indexed. If no other link than this Javascript link points to the target page, it may never be indexed by the robots.

Limits and dangers of redirects

Duplicate content

If, due to a badly managed redirection, the robots find two pages, at two different addresses, which contain the same content (it is generally agreed that it is necessary to ensure that there is less than 70% concordance), they consider that there is no need to index both of them. Depending on the robots or the circumstances, it may happen that :

  • One of the two pages is not indexed (although it is not necessarily possible to know in advance which one will be chosen)
  • The popularity (PageRank in the case of Google) is divided in two for each page

This situation, known as “duplicate content” cannot theoretically occur when using RedirectPermanent with the .htaccess file or when using Php, Asp and Cold Fusion redirects, but it can occur with RewriteEngine, the “Refresh” META tag and when misunderstanding and using Javascript redirects.

As long as your redirection system is in place, it doesn’t matter whether sites that talk about you refer to your old or new addresses. The engines will transfer all the benefits of these “backlinks” to your new addresses and the Internet users will manage to find you in any case.


If, on the other hand, you are forced to remove – in the medium or long term – a redirection system, you must first contact all the webmasters of the sites that mention you to ask them to update their links. Without this step, you will lose part of your referencing the day your redirection system is no longer operational.

Conclusion

Redirects should not be treated lightly, especially the first few times you do them. We hope that this page will help you juggle your URLs with confidence.

Leave a Comment

Would you like to work with our team?
We are ready to welcome you!