I am implementing urlReWriter into my Java web project.
I want to change this url: /read-post.jsp?id=1&title=some-cool-blog-title
into this shortened/cleaner url: /read-post/1/some-cool-blog-title
This is the rule I have implemented:
<rule>
<from>^/read-post/([0-9]+)/([0-9][a-z][A-Z]+)</from>
<to>/read-post.jsp?id=$1&title=$2</to>
</rule>
The problem is it isn't re writing the url and I suspect it is because the xml regex I've used is incorrect?
How do i format it correctly when there can be any number for the id and any number, character or special character - for the title?
Your regular expression for the title ([0-9][a-z][A-Z]+) is for sure not correct since the + refers to the [A-Z] only. In addition to this the - your are mentioning in the question is missing. You could try this instead: ([0-9a-zA-Z\-]+)
Related
Im using struts2 framework and mongoDB database. now i have this URL:
http://localhost:8084/testURL/pages.jsp?pid=5b2d12a67f7eef1094c6a6de
I wanna shows this URL
http://localhost:8084/testURL/pages/5b2d12a67f7eef1094c6a6de .
I am using tuckey for url rewrite. And following is my urlrewrite.xml. returns 404 not found
<urlrewrite>
<rule>
<from>^/pages.jsp?pid=$1</from>
<to>/pages/$</to>
</rule>
</urlrewrite>
Disclaimer: writing as an answer as assumed to be fixing the question and because of the length of the comment
Regex anchors
In regex, ^ and $ are reserved character to say begin and end. I assume that you use the standard regex matching (1) in your URL rewriting rules
Regex capturing
You might need to capture the parameter and then re-use it in the to parameter:
<urlrewrite>
<rule>
<from>^\/pages\.jsp\?pid=(.*)$</from>
<to>/pages/$1</to>
</rule>
</urlrewrite>
All the backslashes because you need to escape special characters: the slash /, the dot . and the question mark ?
Using the dot in (.*) is pretty overkill, feel free to fine-tune the regex. Example below
If you are sure that your PID is always made of alphanumeric lowercase characters, you can use
<urlrewrite>
<rule>
<from>^\/pages\.jsp\?pid=([a-z0-9]*)$</from>
<to>/pages/$1</to>
</rule>
</urlrewrite>
You can test your Regex here: https://regex101.com/
Note
In Tuckey docs(1), it is mentioned that your situation is used with <outbound-rule>. I haven't pushed Tucket that far so I can't tell if using <rule> is enough in your scenario:
<outbound-rule>
<from>^/world.jsp?country=([a-z]+)&city=([a-z]+)$</from>
<to>/world/$1/$2</to>
</outbound-rule>
1: Tuckey documentation: http://tuckey.org/urlrewrite/manual/3.0/
I want use spring filter this url:
http://localhost:9280/spring-filter/login.html?collection=abc#/input
I use code filter
<security:filter-chain pattern="/*/input" filters="collectionLoginFilter" /> but not working.
If I don't have symbol '?' in url http://localhost:9280/spring-filter/login.html/input, it will working.
Question: I want use symbol '?'. What should I do?
I don't think you can. Spring only analyze the URL before '?'. Anything after '?' is parameter
Check spring-security documentation, specifically these line
Un-normalized requests are automatically rejected by default, and path parameters and duplicate slashes are removed for matching purposes
I am trying to match strings v1 and v2. For that, I am trying the following regex : ^v(1|2) (I also tried with $ which is probably what I need). When I test it in http://www.regextester.com/, it seems to work fine. But when I used it in JAX-RS path expression it doesn't work. The expression I use is below:
#Path("/blah/{ver:^v(1|2)}/ep")
Is there anything specific to JAX-RS that I am missing?
Your attempt does not work because of the anchor ^. Quoting from the JAX-RS specification, chapter 3.7.3 (emphasis mine):
The function R(A) converts a URI path template annotation A into a regular expression as follows:
URI encode the template, ignoring URI template variable specifications.
Escape any regular expression characters in the URI template, again ignoring URI template variable specifications.
Replace each URI template variable with a capturing group containing the specified regular expression or ‘([ˆ/]+?)’ if no regular expression is specified.
If the resulting string ends with ‘/’ then remove the final character.
Append ‘(/.*)?’ to the result.
Because each URI templates is placed inside a capturing group, you can't embed anchors in it.
As such, the following will work and will match v1 or v2:
#Path("/blah/{ver:v[12]}/ep")
Try the following (without anchors):
#Path("/blah/{ver : v(1|2)}/ep")
Also, if the change is a single character only, use character set instead of the | operator:
#Path("/blah/{ver : v[12]}/ep")
I want to extract Android application ID from URL.
Examples of URLs are:
https://play.google.com/store/apps/details?id=com.opera.mini.native
https://play.google.com/store/apps/details?id=com.opera.mini.native&referrer=xxxx
And I want to get com.opera.mini.native substrings form both URLs.
I tried to create regex to parse ID, but unsuccessfully:
^.+details\?id=(.+)&?.+
The problem is that regex returns com.opera.mini.native&referrer=xxxx for second case (for 1st URL it works fine).
How I can change regex to achieve my goal?
Thanks
It's because you made & as optional.
".+\\bdetails\\?id=([^&]+)"
Regex can be:
(?<=[?&]id=)[^&]+
ResEx Demo
Below is my regular expression :-
\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]\\b
when the request url is of type http://www.example.com/ , the last character is not replaced in my shortner url and / is appended at end.
The regex is not able to find the last /.
Please help with this.
I think that / would be a word boundary, so maybe it works better if you add a ? to the and, so it reads:
\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]\\b?
what about:
if(url.endsWith("/"))
url = url.substring(0,url.length()-1);
or if you need to use regular expressions you can do something like this:
url = url.replaceAll("(\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*)/(\\b?)","$1$2");
If all you want is to replace the trailing / (which is what your question directly asks), you can simply do:
url = url.substring(0, url.lastIndexOf('/'));
Remember to KISS often.
You could simply use:
url = url.replaceAll("\/+$","");