Issue
- My friendlyURL portlet is not parsing correctly some of my URLs.
- Sometimes it is not clear which kind of pattern we must define in routes.xml when implementing a module to make URLs friendly (see additional information about this implementation).
- For example, if my routes.xml looks like:
<routes>
<route>
<pattern>/{groupId}/{articleURLTitle}</pattern>
</route>
</routes> - The following URL is not going to be parsed correctly:
http://localhost:8080/detail/-/content/20125/test.cc
- And we can see in the log a message like this one (if the DefaultFriendlyURLMapper class' log level is configured to WARN):
2021-07-26 14:46:06.891 WARN [http-nio-7100-exec-10][DefaultFriendlyURLMapper:150] No route could be found to match URL /20125/test.cc - This is because the pattern defined in routes.xml is not correct, so it makes Liferay's choose an unsuitable matcher.
Environment
- Liferay DXP
Resolution
- It is important to know which kind of URLs your friendlyURL portlet is going to handle, since the patterns defined in file routes.xml are the key to apply the correct matcher for them.
- Liferay's available matchers are:
- Each of them define their specific matching rules: for example, DefaultMatcher does not allow the URL to contain dots (".").
- The logic applied to use one or other is defined here.
- In the previous case, since the pattern does not include a colon (":"), the DefaultMatcher is set. And since this matcher "rejects" URLs including dots:
if ((c == CharPool.SLASH) || (c == CharPool.PERIOD)) {Our URL http://localhost:8080/detail/-/content/20125/test.cc is not parsed.
return false;
} - To get our URL including dots parsed, we should define for example the following pattern:
<pattern>/{groupId}/{templateKey}/{articleURLTitle:[^/]+}</pattern>
So the NotSlashMatcher is selected and the parsing does no reject the dots.
Additional Information
- https://help.liferay.com/hc/es/articles/360017885992-Making-URLs-Friendlier
- StringParserFragment.java logic.