Using Pattern Redirects
Available 7.4 U48+/GA48+
Pattern redirects use regular expressions to define patterns for multiple source URLs and their replacements. Consequently, they are best used when you have large groups of URLs to map to new URLs. You can create bulk redirects in a web server like Nginx or Apache, but since this is an out-of-the-box feature, you can set up pattern redirects directly in Liferay.
Because Liferay interprets redirect patterns as friendly URLs based on the current site’s friendly URL, you cannot use redirect patterns with non-friendly URLs (e.g., virtual host names, port numbers) or redirect to pages outside of the current site’s scope (i.e., redirect from page A in site A to page B in site B).
However, you can redirect to external websites by adding the protocol before the address. (e.g., use http://www.externalwebsite.com to redirect to the external website).
Depending on the regular expression you use, pattern redirects can be slower than alias redirects and result in more URL matches than intended. For these reasons, alias redirects are recommended when possible. See Setting Up Redirects and 404 Tracking for more information.
Creating Pattern Redirects
-
Open the Site Menu ( ) and go to Configuration → Redirection.
-
Go to the Patterns tab.
-
In the Pattern field, enter a regular expression for the source URL. It’s implicitly anchored to the beginning of each page’s friendly URL, so
/home
is equivalent to^/home
in regex.Liferay supports all operations provided by the Java regular expression package. See the JDK documentation for more information.
-
Enter a destination URL.
tipYou can use references to capture groups of URLs in the replacement pattern. For example, in the following image, the
$1
value is replaced with the group of values captured by the(\d+)
regular expression. -
(Optional) Click the Plus button to add multiple redirect patterns for the site.
-
Click Save.
Avoid creating circular redirects. These are easy to create when defining redirects for content friendly URLs (e.g. redirecting between different friendly URLs for the same content). Liferay cannot reliably detect these loops.
Creating Pattern Redirects: Changing Domains
If you have two domains such as https://www.old-site.com and https://www.new-site.com, you can redirect a page from one domain to the other:
-
Navigate to https://www.old-site.com.
-
Open the Site Menu ( ) and go to Configuration → Redirection.
-
Under Pattern, add
^(.*)
.This regular expression captures the entire URL path of the source request.
-
Under Destination URL, add
https://www.new-site.com/$0
.$0
appends the entire captured URL path from the source to the destination URL. -
Click Save.
Now, if you go to https://www.old-site.com/examplepage, you are redirected to https://www.new-site.com/examplepage.
Understanding Pattern Redirects
When a user attempts to access the source URL, Liferay tests the patterns against the layout friendly URL in the order specified. The matching process roughly follows these rules:
-
If the URL is a control panel URL, no matching is performed, since it is impossible to capture control panel URLs.
-
If there is an alias redirect for the current URL, the user is redirected to that destination.
-
If no alias is found, Liferay tests each redirect pattern in sequence until all patterns are exhausted or one of them matches. Liferay redirects users to the first match.