I'm trying to use tuckey URL rewriter and java to redirect all URLs with a certain request parameter. So for example I'd like to convert all of the following:
http://xxx.com/?refcode=1234
to simply http://xxx.com/
First of all is tuckey url rewriting the best way to go about this? Using tuckey, I've managed to match URL such as ...co/refcode=1234 (without the ?) using something like this:
<rule>
<note>Remove agent ID from URL without query parameters</note>
<from>^/.*refcode=.*$</from>
<to type="redirect">/redirect.do</to>
</rule>
but I can't seem to write a regex that would match the ? as well. I've tried escaping the ? (using \?) and I've also tried to use regexs with [?] with no luck. So something along these lines doesn't seem to work:
<rule>
<note>Remove agent ID from URL without query parameters</note>
<from>^/[?]agentId=.*$</from>
<to type="redirect">/bank.do</to>
</rule>
OR
<rule>
<note>Remove agent ID from URL without query parameters</note>
<from>^/\?agentId=.*$</from>
<to type="redirect">/bank.do</to>
</rule>
Would really appreciate if someone can let me know how I can go about writing the correct regex to match this URL
\? is the correct way to escape the ?, I would avoid using the $ at the end.
<from>^/\?refcode=(\d+)</from>
You could also use the CONDITION tag
<condition type="query-string">refcode=(\d+)</condition>
<from>/</from>
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 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\-]+)
I need a regex pattern that will find and replace brackets in urls to its urls encoding.
For example a base url like:
http://www.mysite.com/bla/blabla/abc[1].txt
will be turned to:
http://www.mysite.com/bla/blabla/abc%5B1%5D.txt
can anyone help please?
EDIT1:
i originaly use commons-httpclient to access this kind of urls.
when I use the first URL I get an "escaped absolute path no valid" exception.
I can't use URLENCODER because when I use it, I get a "host parameter is null" exception.
The following line should do the trick
String s = URLEncoder.encode("http://www.mysite.com/bla/blabla/abc[1].txt", "UTF-8");
Have you tried URLEncoder.encode?
in the java.net.URLEncoder package.
EDIT:
Ok i see... you cannot pass an entire URL to URLEncoder. URLEncoder is mostly used to encode query parameters.
try this instead:
URI uri = new URI("http", "www.mysite.com", "/bla/blabla/abc[1].txt",null);
System.out.println(uri.toASCIIString());
I have the following rule:
<rule>
<from>^/users/(.*)$</from>
<to last="true">/users.do$1</to>
</rule>
And I want to match the following url:
http://localhost:8077/users/?elemsPerPage=10
and redirect it to:
http://localhost:8077/users.do?elemsPerPage=10
The problem is that when the url rewriter engine finds the "?" character in the url it does not return anything else in the $1 matched parameter. Neither it adds the parameters to the query string. Any ideas?
Finally I've found a way to solve this:
Rule:
<rule>
<from>^/users/$</from>
<to last="true">/users.do?%{query-string}</to>
</rule>
There is a better way, try to add use-query-string="true" in urlrewrite
<urlrewrite use-query-string="true">