Displaying items by tag: features

Friday, 25 October 2013 00:00

Redirection rule features

As mentioned earlier the "From URL" field can contain a regular expression and can match the whole URL or just a part of it. The following table shows some regexp examples.

"From URL" fieldWhat does it mean
/section/*$ The dollar ($) means "end with". The star (*) means "match zero or more occurrences of previous character", that is the slash (/). So this rule means "address ends with /section or /section/".
^/section/9-article\.html$ The caret (^) means "start with". The dollar ($) means "end with". So the rule means "address starts and ends exactly with /section/9-article.html".
^/section/11-articles\.php\?id=12$ The caret (^) means "start with". The dollar ($) means "end with". Also, we need a "literal" question-mark (?) for inline arguments, so it must be escaped with a backslash (\?). So the rule means "address starts and ends exactly with /section/11-articles.php?id=12". Note that any special char needs to be escaped with a backslash to be literal, so we should escape also the dot (.) char. If not escaped, the dot will match with "any char".

The rules ordering is important for matching the source URL. For each URL request, all published rules are compared according to their ordering. Even if two or more rules match, ONLY THE FIRST ONE in the rule ordering will be used. This can be useful, as example, to distinguish a category from its articles. If we have two URLs as follow:

http://www.mysite.com/category/
http://www.mysite.com/category/article-1.html

Rules could be configured with the following "From URL" field:

Item OrderWrong OrderCorrect Order
5 /category/ /category/article-1.html
6 /category/article-1.html /category/

 

For http://www.mysite.com/category/article-1.html both rules match this URL, but only the first matching rule is applied so, if rules are configured in the wrong order, the redirect for page article-1.html will never be used.

Internal Redirect

This feature is available only in the Enterprise version of ReDJ. The 301 (Moved Permanently) or 307 (Temporary Redirect) redirects are client-side redirects. The user from his web browser requests a page (the source URL), and the web server responds with the address to which the page was moved (the destination URL). At this point, it is the client browser that makes a new request for the new address (i.e. is the client that performs the redirect).
With the "Internal Redirect" instead, is the web server that changes the page returned to web browser with the new destination. The result is a server-side redirect (i.e. the redirect happens "internally" to Joomla), and the user does not notice any URL change in the address bar. This is a great solution to create URL aliases and short URLs. Just as example, let's consider an existing URL like the following:

http://www.yoursite.com/index.php/component/reservation/event/4/forum

It is possible to create a rule with a redirection type 200 (Internal) that match to the following URL:

http://www.yoursite.com/training

The page content sent to client is exactly the same for both URLs, but the shortest URL is much friendly and better for SEF and SEO.

Dynamic Destinations (Macros and Placeholders)

Let's take a deeper look at "To URL" destination field. Since version 1.5 of ReDJ, a support for complex "macros" has been added, and this allows to create dynamic destinations. The version 1.7.1 introduces support for "placeholders", that adds a new dimension to build complex URL transformations.

When the URL request matches the "From URL" source field of a rule, if the destination field "To URL" contains one or more macros or placeholders, then ReDJ first calculates and replaces any placeholder and macro with its current value, builds the final "real destination" URL, and finally performs the redirect.

Macros allow, as example, to manipulate pieces of the current matching URL (e.g. a query variable), placeholders can define new variables using constant strings, macros and other (previous) placeholders, so both let you to create dynamic destinations as complex transformations of the current source URL.

Here a practical example of dynamic destinations and macro substitution:

Source URL http://www.yoursite.com/old/index.php?task=view&id=123&ItemId=3
ReDJ "To URL" http://{siteurl}/new.html{querybuild ItemId=12}
Destination URL http://www.yoursite.com/new.html?task=view&id=123&ItemId=12

 

Placeholders allow you to define a set of values where each value is associated and identified by a name. A placeholder can be used in the definition of other placeholders or in the definition of meta info (practically they are based on the same principle of the variables in programming languages). Syntax for defining a placeholder is name=value where 'name' is the placeholder identifier (placeholder valid name, like variables in PHP convention, starts with a letter or underscore, followed by any number of letters, numbers, or underscores), while 'value' can be any string, and can also contains macros or placeholders defined before, and both will be replaced with their current value. The syntax to use a placeholder is ${name}, where 'name' is the name of the placeholder. Each rule has a text area field to define any number of placeholders, one for each row. For example, if you enter:

a=this
b=${a} is
c=${b} an
d=${c} example

Putting the placeholder ${d} in a meta field (e.g. title or description), this field will assume the value 'this is an example'. Here is a practical example of how to use the placeholders to get a complex result through the use of available macros in a sequence of definitions. Let's assume we need to create a rule for URL:

http://www.mysite.com/index.php?option=com_content&view=article&id=53&Itemid=280

We want to withdraw the article id (53), and perform a look-up on the database for getting the article alias (using-joomla), to be used for example in the page title. We can define the following placeholders:

id={preg_match 1}/option=com_content&view=article&id=([0-9]+)/i{/preg_match}
alias={tableselect #__content,alias,id}${id}{/tableselect}

After this, putting ${alias} in the title field, this will be replaced by 'using-joomla' in the page.

Published in ReDJ for Joomla 2.5