How to create a 301 redirect with rewrite rules?
Apache server configuration allows you to set up 301 redirects by using the Mod_Alias by default installed on your server. But it is also possible to set up your redirects a more generic way by using Rewrite rules. Rewrite rules are originally used to write user friendly URL on a website, and help in the Search optimisation of a website. Used with right parameters and options, it can be used to define 301 redirect.
Setting up a 301 redirect using rewrite rules on an Apache server requires a precise configuration. First, you have to be certain that the Mod_Rewrite module is well installed on your server. This module has to be installed on an Apache 1.3 configuration, but is by default set up under Apache 2.0 and 2.2.
Once this module activated on your Apache server, the definition of 301 redirects is only a matter of some command lines defined in a .htaccess file on the root of your website. On an Apache server configuration, this .htaccess file contains all the commands and controls your server must execute before accessing a file located within the same directory. By placing your redirection process into the .htaccess file located at the root of your site, you're certain it will be checked before any access to a file of your site.
Setting up the redirection.
In order to set up rewrite rules on your website, your .htaccess file must look like the following sample:
RewriteRule ^old\.html$ new.html [R=301]
The two first lines of the file, RewriteEngine on and RewriteBase /, help to indicate that you'll define rewrite rules on the following command of the file and that any relative URL reference will be defined following the root of your domain name.
Once those two lines written, you can then define as many rewrite rules and 301 redirections as you wish. Simply keep the following structure:
- Begin each new rule with RewriteRule command in order to indicate you begin a new rule;
- Then, enter the name of the file from which you want to set up the redirection. This name is the relative path to this file, beginning with a ^ character and finishing with a $;
- Enter then the destination URL of your redirection, also in relative path;
- The last part of your rule must be [R=301] in order to indicate that your redirection process is using the 301 code.
How to use regular expression on the redirect definition?
One of the biggest advantageq of setting up 301 redirect using rewrite rules is that you can define dynamical redirection using Regular expression. This way, you don't have to create a redirect rule for each page, but you can create a generic redirect rule for a specific scheme of URL. For example, if you want to create a redirection for all the product pages of your site from an old PHP style URL to a new optimised one, you can use the following rule:
RewriteRule ^product.php?id=([0-9]+)(/)?$ product/$1 [R=301]
This rule is understood this way by the server: when trying to access http://www.mysite.com/product.php?id=125, the user is automatically redirected to http://www.mysite.com/produit/125/. The $1 string stands for a dynamical inclusion of the regular expression defined earlier in the new URL. In this example, it simply means that in the new URL, $1 will be replaced by the string found in place of ([0-9]+) in the first address.
- [0-9]: numeric character
- [a-z]: low-case alphabetical character
- [A-Z]: high case alphabetical character
- [A-Za-z]: alphabetical character, high- or low-case
- [0-9A-Za-z]: alphanumerical character, high- or low-case
- ?: Zero or one occurrence of previously defined string
- *: Zero or more occurrence of previously defined string
- +: One or more occurrence of previously defined string
You can learn more about regular expressions and their usage by reading this online tutorial.