Introduction
Redirections of non-existing pages to existing pages can easily be configured within your web site. This also includes the presentation of the list of available languistic versions of a page in case the requested version does not exists.
How it works
When a non existing page is requested on the server, the webserver calls a program that will analyse what can be done depending of rules present in configuration files.
The program will look for a _redirect.cfg file in the directory of the requested address. If not found, the program will recursively scan the parents directories until it finds a _redirect.cfg file.
The _redirect.cfg file tells the program how to map the requested address to a new address with rules. If none of the rules matches the requested address, the program will try to find a matching rule in the _redirect.cfg of the parent directory tree.
When the first matching rule is found, the program will optionnaly present a page - based on a template - that will show the requested address and the new computed address. This would help the users to update their bookmarks.
An optional automatic timed redirection can be programmed as well.
Configuring the _redirect.cfg file
The _redirect.cfg is a text file with rules on separated lines.
Each line contains 3 columns representing a rule.
The first column is a regular expression that the program tries interpret according to the requested address (URL) . If the rule matches the address then the program will redirect the browser to the page given in the second column.
The second column is thus the new address to which the browser will be redirected.
The third column is a code that tells the behaviour of the program:
- code > 0 : number of seconds before automatic redirection
- code = 0 : hyperlink with no automatic redirection
- code = -1 : no hyperlink and no automatic redirection
- code = -9 : immediate redirection
- code = -999: linguistic research
Sample _redirect.cfg file
# sample _redirect.cfg file. | ||
# | ||
#<From URL> | <to URL> | <CODE> |
# | ||
# simple redirections | ||
# redirect calls to myoldfile.htm to /mynewfile.htm | ||
myoldfile.htm | /mynewfile.htm | 0 |
# redirect calls to all files in dir1 to index.htm in /dir1 | ||
dir1/.* | /dir1/index.htm | 0 |
# redirect calls to all files contained in any dir2 to index.htm in /dir2 | ||
.*dir2/.* | /dir2/index.htm | 0 |
# | ||
# redirections using regular expressions | ||
# redirect calls to all files with .html suffix to same file with .htm suffix | ||
(.*)$0.html | $0.htm | 0 |
# redirect calls to .pdf files to a new subdirectory "pdf" at the lowest level | ||
(.*)$0/(.*)$1.pdf | $0/pdf/$1.pdf | 0 |
# | ||
# linguistic redirections: the second column contains the name of the template file. # please note that the rule must contain one occurence of (..) that represent the iso-alpha-2 code of the language | ||
# redirect call to any vl of .html or .htm file that contains a language code (that must be defined in the template file) to a new page based on the template. | ||
.*_(..)$0.html(0,1) | lang_template_$0.htm | -999 |
# redirect non existing linguistic versions of a page to the english version of a new page based on the template. | ||
.*_(..)$0.htm | lang_template_en.htm | -999 |
# | ||
# default redirection | ||
# default redirection to a default page "not_found.htm". If this rule is not present, the next _redirect.cfg config file will be scanned in the parent directories to find a matching rule. | ||
.* | /comm/agriculture/not_found.htm | 0 |
Configuring the _redirect.htm template
The _redirect.htm template is used to present the user with a text showing the requested address that doesn't exists anymore and the new computed address. This should help the user to update locally stored bookmarks.
The _redirect.htm template file should be placed in the same location as the _redirect.cfg file
The _redirect.htm template file is a standard IPG-compliant HTML file with special tags that will be replaced by the program before beeing displayed to the the browser:
<!--OLDADDR--> | This tag will be replaced by the requested address that doesn't exists anymore |
<!--NEWADDR--> | This tag will be replaced by the new computed address |
An optional text can also be placed between the following tags: <!--AUTOSTA--> and <!--AUTOEND-->. This text will be displayed when an automatic timed redirection is requested.
Sample _redirect.htm template file
<HTML> <HEAD> <TITLE>Page not found</TITLE> <BASE HREF="http://europa.eu.int/comm/agriculture/_redirect.htm"> </HEAD> <BODY> <CENTER><H1>Welcome to the European Commission</H1></CENTER> <CENTER><P>The page you have requested:</P> <P><!--OLDURL--></P> <P>is not available, or has been moved. You should try:</P> <P><B><!--NEWURL--></B></P> <!--AUTOSTA--> You will be redirected to the new address in a few seconds <!--AUTOEND--></CENTER> </BODY> </HTML>
Configuring the linguistic templates (lang_template_xx.htm in the example)
Sample linguistic template