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 linguistic versions of a page in case the requested version does not exist.
Redirections can be useful to keep active an old addressing scheme. In case of site restructuration, the main parts of the site could be redirected to their equivalent part in the new site.
Various behaviours are available to progressively encourage people to update their bookmarks. It goes from the direct redirection without notification to the display of a page with the non-clickable address of the new site.
Restriction: Only non-existing pages can be redirected by this mean.How it works
When a non existing page is requested from the server, the webserver calls a program that will analyse what can be done depending on rules set 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 parent 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 optionally 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.
Basic redirections
Configuring the _redirect.cfg file
The _redirect.cfg is a text file with rules defined on separated lines.
Each line contains 3 columns representing a rule.
The first column is a regular expression that the program tries to 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. This new address should be relative to the directory of the _redirect.cfg file.
It is also possible to specify fully qualified addresses, but then the existence of the page cannot be tested by the tool. This could however be useful to redirect to dynamic applications or to external websites.
The third column is a code that indicates 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 search
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 occurrence of (..) that represent the iso-alpha-2 code of the language | ||
# redirect call to any linguistic version 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} | template_lang_$0.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 | -9 |
Configuring the _redirect[_xx].htm template files
The _redirect[_xx].htm template files are 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[_xx].htm template files should be placed in the same location as the _redirect.cfg file
You can have one template file per language (_redirect_[xx].htm) and one additional template for non-existing files that doesn't contain a language code in their name (_redirect.htm)
Please note that those template files are not used for linguistic research (code = -999).
The _redirect[_xx].htm template files are standard IPG-compliant HTML files with special comment tags that will be replaced by the program before being sent to the browser:
<!--OLDURL--> | This tag will be replaced by the requested address that doesn't exists anymore |
<!--NEWURL--> | This tag will be replaced by the new computed address |
An optional text can also be placed between the following tags: <!--AUTOTEXT--> and <!--/AUTOTEXT-->. This text will be displayed when an automatic timed redirection is requested.
Sample _redirect_en.htm template file
<HTML> <HEAD> <TITLE>Page not found</TITLE> <BASE HREF="http://europa.eu.int/comm/agriculture/_redirect_en.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> <!--AUTOTEXT--> You will be redirected to the new address in a few seconds <!--/AUTOTEXT--></CENTER> </BODY> </HTML>
Linguistic redirections
Configuring the linguistic templates (template_lang_xx.htm in the example)
The linguistic template files are used to present the user with a page showing the available/not available linguistic versions of page.
By default, the linguistic template files should be placed in the same directory as the _redirect.cfg file. It is also possible to specify the path of the template files.
You should have one template file per navigation language in your site. This means that the text explaining that the requested linguistic version does not exists will be written in the navigation language.
You can also give a single template file in a default language.
The linguistic template files are standard IPG-compliant HTML files with special comment tags that will be replaced by the program before being sent to the browser:
The following syntax is used to describe and link a <language> version of a file:
<!--LANG NAME="<language>" FOUND="yes"-->
- unlinked text part 1 <!--LINK-->linked text<!--/LINK--> unlinked text part 2
The following syntax can be used when a <language> version of a file is not found:
<!--LANG NAME="<language>" FOUND="no"-->
- unlinked text
Please note that only the linguistic versions that have an entry in the template files (<!--LANG NAME="<language>") will be presented in the list of available versions.
Sample linguistic template
<HTML> <HEAD> <TITLE>Page not found</TITLE> <BASE HREF="http://europa.eu.int/comm/agriculture/template_lang_en.htm"> </HEAD> <BODY> <CENTER><H1>Welcome to the European Commission</H1></CENTER> <P>The page you have requested does not exist in English.</P> <!--LANG NAME="es" FOUND="yes"-->please click to access the <!--LINK-->Spanish<!--/LINK--> version<!--/LANG--> <!--LANG NAME="fr" FOUND="yes"-->please click to access the <!--LINK-->French<!--/LINK--> version<!--/LANG--> <!--LANG NAME="de" FOUND="yes"-->please click to access the <!--LINK-->German<!--/LINK--> version<!--/LANG--> </BODY> </HTML>
Life examples - Demonstrations
The examples are based on a demo subsite with the following content:
demo
- 404.htm
404_fr.htm
_redirect.cfg
_redirect.htm
_redirect_en.htm
_redirect_fr.htm
template_lang.htm
template_lang_en.htm
template_lang_fr.htm
demo/newsite
- index.htm
index_fr.htm
not_found.htm
demo/pdf
- c_el.pdf
c_es.pdf
c_lv.pdf
Code=-9 : Immediate redirection
In this example, page addresses from an "oldsite" are mapped directly to a "newsite"
If a user request a page in the oldsite and that page exists in the newsite, there is an immediate redirection (demo/oldsite/index.htm).
If the page does not exist in the newsite, there is a redirection to a newsite/not_found.htm page (demo/oldsite/nofile.htm).
The matching rules of the _redirect.cfg file are:
<From URL> | <To URL> | <Code> |
oldsite/(.*)$0 | newsite/$0 | -9 |
oldsite/.* | newsite/not_found.htm | -9 |
There are no additional files used (no template files).
Code=>0 : timed automatic redirection
In this example, when a user request a page that does not exist, a template page is displayed in which the requested address and the suggested new address are shown and the new page is loaded after the number of seconds indicated by the code (demo/timesite/index.htm).
The template is selected according to the language of the non-existing requested page (demo/timesite/index_fr.htm).
The matching rule of the _redirect.cfg file is:
<From URL> | <To URL> | <Code> |
timesite/(.*)$0 | newsite/$0 | 5 |
There are one template file per language (_redirect_[xx].htm) and one multilingual template file used (_redirect.htm)
Code=0 : hyperlink with no automatic redirection
In this example, when a user request a page that does not exist, a template page is displayed in which the requested address and the suggested new address are shown (demo/prevsite/index.htm).
The template is selected according to the language of the non-existing requested page (demo/prevsite/index_fr.htm).
The matching rule of the _redirect.cfg file is:
<From URL> | <To URL> | <Code> |
prevsite/(.*)$0 | newsite/$0 | 0 |
There are one template file per language (_redirect_[xx].htm) and one multilingual template file used (_redirect.htm)
Code=-1 : no hyperlink and no automatic redirection
This example is the same as the previous one, except that the new url is not active anymore.
(demo/nosite/index.htm).
(demo/nosite/index_fr.htm).
The matching rule of the _redirect.cfg file is:
<From URL> | <To URL> | <Code> |
nosite/(.*)$0 | newsite/$0 | -1 |
There are one template file per language (_redirect_[xx].htm) and one multilingual template file used (_redirect.htm)
Code=-999 : linguistic search
In this example, the user request a pdf file that does not exist in english or french. A template-based page is generated listing the available language version of the pdf file.
(demo/pdf/c_en.pdf).
(demo/pdf/c_fr.pdf).
If the user request a non-existing pdf file of another language, the default template is used.
(demo/pdf/c_it.pdf).
The matching rule of the _redirect.cfg file is:
<From URL> | <To URL> | <Code> |
.*_(..)$0.pdf | template_lang_$0.htm | -999 |
.*_(..)$0.pdf | template_lang.htm | -999 |
There is one template file per navigation language (_template_lang_[xx].htm) used.