Displaying items by tag: rule

Monday, 11 November 2013 00:00

Supported macros

Referring to an URL like the following:

ReDJ URL parts

Here is a list of supported macros for destination "To URL" field:

MacroDescription
URL macros
{siteurl} Web site address
Example

www.example.com/path/to/Joomla

{scheme} URL scheme
Example

http

{host} URL host
Example

www.example.com

{port} URL port number
Example

8080

{user} User name
Example

fredbloggs

{pass} User password
Example

itsasecret

{path} URL path
Example

/path/to/Joomla/section/cat/index.php

{query} URL query string
Example

task=view&id=32

{queryfull} URL query string full (with question mark)
Example

?task=view&id=32

{querybuild var1=value1,...,varN=valueN} Builds URL query containing only specified variables. If a value is specified for a variable, this is assigned to the variable
Example

{querybuild id,task} => id=32&task=view
{querybuild id,task=edit} => id=32&task=edit
{querybuild id,task=view,ItemId=12} => id=32&task=view&ItemId=12

{querybuildfull var1=value1,...,varN=valueN} Same as 'querybuild' but with question mark
Example

{querybuildfull id,task} => ?id=32&task=view
{querybuildfull id,task=save} => ?id=32&task=save
{querybuildfull id,task,action=close} => ?id=32&task=view&action=close

{querybuildappend var1=value1,...,varN=valueN} Same as 'querybuild' but with ampersand
Example

{querybuildappend id,task} => &id=32&task=view
{querybuildappend id,task=save} => &id=32&task=save
{querybuildappend id,task,action=close} => &id=32&task=view&action=close

{querydrop var1=value1,...,varN=valueN} Builds URL query removing specified variables from the original query. If a value is specified for a variable, this is assigned to the variable (that is not dropped) - ENTERPRISE VERSION ONLY
Example

{querydrop task} => id=32
{querydrop id,task=edit} => task=edit
{querydrop id,task=save,action=close} => task=save&action=close

{querydropfull var1=value1,...,varN=valueN} Same as 'querydrop' but with question mark - ENTERPRISE VERSION ONLY
Example

{querydropfull task} => ?id=32
{querydropfull id,task=save} => ?task=save
{querydropfull id,task=edit,action=close} => ?task=edit&action=close

{querydropappend var1=value1,...,varN=valueN} Same as 'querydrop' but with ampersand - ENTERPRISE VERSION ONLY
Example

{querydropappend task} => &id=32
{querydropappend id,task=save} => &task=save
{querydropappend id,task=edit,action=close} => &task=edit&action=close

{queryvar varname,default} Returns the current value for the variable 'varname' of the URL, or the value 'default' if 'varname' is not defined (where default = '' when not specified)
Example

{queryvar task} => view
{queryvar id} => 32
{queryvar maxsize,234} => 234

{requestvar varname,default} Returns the current value for the variable 'varname' of the request, no matter about method (GET, POST, ...), or the value 'default' if 'varname' is not defined (where default = '' when not specified)
Example

{requestvar id} => 32
{requestvar limit,100} => 100

{authority} URL authority string
Example

fredbloggs:itsasecret@www.example.com:8080

{baseonly} Returns the base path only (empty when installed on root, i.e. it will never contains a trailing slash)
Example

/path/to/Joomla

{pathfrombase} Returns the path without the base part
Example

/section/cat/index.php

{pathltrim pathtotrim} Strip the string specified (pathtotrim) from the beginning of the path and returns the result
Example

{pathltrim /path/to} => /Joomla/section/cat/index.php

{pathrtrim pathtotrim} Strip the string specified (pathtotrim) from the end of the path and returns the result
Example

{pathrtrim /index.php} => /path/to/Joomla/section/cat

{pathfrombaseltrim pathtotrim} Same as 'pathltrim' but works on path without the base part - ENTERPRISE VERSION ONLY
Example

{pathfrombaseltrim /section} => /cat/index.php

{pathfrombasertrim pathtotrim} Same as 'pathrtrim' but works on path without the base part - ENTERPRISE VERSION ONLY
Example

{pathfrombasertrim index.php} => /section/cat/

{preg_match N}pattern{/preg_match} Returns the N-th matched pattern on the 'full source URL', where N = 0 when not specified - ENTERPRISE VERSION ONLY
Example

{preg_match}/([^\/]+)(\.php|\.html|\.htm)/i{/preg_match} => index.php
{preg_match 2}/([^\/]+)(\.php|\.html|\.htm)/i{/preg_match} => .php

{routeurl} Returns the routed (relative) URL using all current variables
Example

/using-joomla.html

{routeurl var1,var2,var3=myvalue,..,varN} Returns the routed (relative) URL for specified variables
Example

index.php?var1=value1&var2=value2&var3=myvalue&..&varN=valueN

{pathfolder N} Returns the N-th folder of the URL path - ENTERPRISE VERSION ONLY
Example

{pathfolder 4} of /path/to/Joomla/section/cat/index.php => section

{pathfolder last-N} Returns the (last-N)-th folder of the URL path, where N = 0 when not specified - ENTERPRISE VERSION ONLY
Example

{pathfolder last-1} of /path/to/Joomla/section/cat/index.php => cat

Site macros
{sitename} Global site name
Example

Global $sitename configuration value

{globaldescription} Global site meta description
Example

Global $MetaDesc configuration value

{globalkeywords} Global site meta keywords
Example

Global $MetaKeys configuration value

{username} Returns the current username
Example

{username} => admin

{username default} Returns the current username or the value 'default' for guest users
Example

{username nobody} => nobody

{userid} Returns the current user ID
Example

{userid} => 62

{userid default} Returns the current user ID or the value 'default' for guest users
Example

{username 1001} => 1001

Database macros
{tableselect table,column,key}value{/tableselect} Returns result of a SQL query (SELECT column FROM table WHERE key = value). Support #__ notation for table name
Example

{tableselect #__users,username,id}135{/tableselect} => SELECT username FROM #__users WHERE id = 135

{preg_select table,column,key,N}pattern{/preg_select} Same as 'preg_match' but uses the matched result to execute a SQL query (SELECT column FROM table WHERE key = matchN). Support #__ notation for table name - ENTERPRISE VERSION ONLY
Example

{preg_select #__content,title,id,1}/id=([0-9]+)/i{/preg_match} => SELECT title FROM #__content WHERE id = 32

{placeholder_select table,column,key1=placeholder1,..,keyN=placeholderN} Returns result of a SQL query (SELECT column FROM table WHERE key1 = ${placeholder1} AND ... AND keyN = ${placeholderN}) where ${placeholderN} is the value of the placeholder with name placeholderN. Support #__ notation for table name. The 'placeholder' must be previously defined (just placeholder name needed, not ${} format) - ENTERPRISE VERSION ONLY
Example

userId=62
{tableselect #__users,username,id=userId} => SELECT username FROM #__users WHERE id = 62

String macros
{substr start,length}text{/substr} Returns the portion of string specified by the start and length parameters. String are zero-based (i.e. first char has index 0)
Example

{substr 1,3}Joomla{/substr} => oom

{strip_tags}text{/strip_tags} Strip HTML and PHP tags from the given string
Example

{strip_tags}<p>hello<br/>Joomla</p>{/strip_tags} => helloJoomla

{extract N}text{/extract} Returns the Nth sentence delimited with carriage return (DOS, UNIX or MAC format). All HTML tags stripped
Example

{extract 1}<p>This is a sentence.\nAnd this is another one.</p>{/extract} => This is a sentence.

{extractp N}text{/extractp} Returns the Nth HTML paragraph <p></p>. All HTML tags stripped
Example

{extractp 2}<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p>{/extractp} => Second paragraph

{extractdiv N}text{/extractdiv} Returns the Nth HTML paragraph <div></div>. All HTML tags stripped
Example

{extractp 3}<div>First paragraph</div><div>Second paragraph</div><div>Third paragraph</div>{/extractp} => Third paragraph

{preg_subject N,subject}pattern{/preg_subject} Returns the N-th matched pattern on the 'subject', where N = 0 when not specified. The 'subject' can contain placeholders, but cannot contain standalone '}' chars - ENTERPRISE VERSION ONLY
Example

{preg_subject 2,First/Second/Third}/([^\/]+)\/([^\/]+)\/([^\/]+)/i{/preg_subject} => Second

{preg_placeholder N,placeholder}pattern{/preg_placeholder} Returns the N-th matched pattern on the 'placeholder' evaluated string, where N = 0 when not specified. The 'placeholder' must be previously defined (just placeholder name needed, not ${} format)
Example

a=First/Second/Third
{preg_subject 2,a}/([^\/]+)\/([^\/]+)\/([^\/]+)/i{/preg_subject} => Second

{lowercase}pattern{/lowercase} Returns the pattern in lower case
Example

{lowercase}ALL to Lower{/lowercase} => all to lower

{uppercase}pattern{/uppercase} Returns the pattern in upper case
Example

{uppercase}ALL to UppeR{/uppercase} => ALL TO UPPER

{urldecode}pattern{/urldecode} Returns the decode pattern using urldecode() function
Example

{urldecode}%2Fgoto%20home%2F{/urldecode} => /goto home/

{urlencode}pattern{/urlencode} Returns the encoded pattern using urlencode() function
Example

{urlencode}/encode me/{/urlencode} => %2Fencode%20me%2F

{rawurldecode}pattern{/rawurldecode} Returns the decode pattern using rawurldecode() function
Example

{rawurldecode}foo%20bar%40baz{/rawurldecode} => foo bar@baz

{rawurlencode}pattern{/rawurlencode} Returns the encoded pattern using rawurlencode() function
Example

{rawurlencode}foo @+%/{/rawurlencode} => foo%20%40%2B%25%2F

{str_replace search,replace}pattern{/str_replace} Returns the pattern after replacing 'search' with 'replace'
Example

{str_replace john,scott}my name is john{/str_replace} => my name is scott

Published in ReDJ for Joomla 2.5
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