URL Rewrite requestURI to NVP - regex

I am trying to write a general rule in /WEB-INF/urlrewrite.xml that will take in an arbitrary URI and append the identifiers that follow a root identifier as Name Value Pairs.
For Example:
localhost:8084/URLRewrite/Fruits/Yellow/Banana/banana.jsp
to become
localhost:8084/URLRewrite/Fruits/Yellow/Banana/banana.jsp&key0=Fruits&key1=Yellow&key2=Banana
I know that this will involve regular expressions as well as parameter ambiguity.
Something of similar flavor is like:
<rule>
<from>^/world/([a-z]+)/([a-z]+)$</from>
<to>/world.jsp?country=$1&city=$2</to>
</rule>
Examples:
Input:
nyc
Output:
nyc
Any help or suggestions would be greatly appreciated.

Try this:
<rule>
<from>^/world/([a-z]+)/([a-z]+)$</from>
<to type="redirect">/world.jsp?country=$1&city=$2</to>
</rule>
Maybe you're missing the attribute type with value redirect in your configuration file.

Related

Using * in regular expression in the action name of struts2

My url may or may not have a certain parameter and I set the action class for such a url as follows :
<action name="{paramOne}/{paramTwo:myparam*}/details"
class="myaction"
method="execute">
<result name="success">/mypage.jsp</result>
</action>
So the url something/myparam/details is working but when i try to invoke something/details (which according to struts.xml should work). It shows that there is not action class mapped.
your code '*' only can match between "{paramOne}/" and "/details"
so you must have something between "{paramOne}/" and "/details"
You want to route different URI patterns to the same action, this is exactly the case action mapping is made for.
I think the best solution is to write two separate statements.
If you don't want to repeat the "code" inside the <action> tag you can chain it (it's a kind of internal redirect, a kind of aliasing)
<action name="secondpattern" class="com.opensymphony.xwork2.ActionSupport">
<result type="chain">firstpattern</result>
</action>
https://struts.apache.org/docs/action-chaining.html

UrlRewriting.net rewrite .htm to extensionless, but ignore umbraco directories

I need to redirect all of the legacy .htm urls for my website to extensionless urls, so:
http://www.website.com/page.htm --> http://www.website.com/page
I created the following rule in config/UrlRewriting.config, which fixed the above issue, but created an issue where all of the backoffice paths are returning 404:
<add name="HtmToExtensionless"
redirect="Domain"
ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="http://www.website.com/(.*)(\.htm)"
redirectMode="Permanent"
destinationUrl="http://www.website.com/$1" />
So requests like these are returning 404:
http://www.website.com/umbraco/views/components/application/umb-navigationl?umb__rnd=7.4.2.1536555776
I'm pretty sure my problem is with my regex, but I'm not sure. Anyone care to advise?
Turns out I was being a dufus. Umbraco actually requests resources that have .html extensions, and my regex was grabbing those and stripping the .htm from them. Just had to check for the beginning and end of the string, and now we're golden:
<add name="HtmToExtensionless"
redirect="Domain"
ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="^http://www.website.com/(.*)(\.htm)$"
redirectMode="Permanent"
destinationUrl="http://www.website.com/$1" />

Tuckey urlrewritefilter rewrite query string

I am trying to use Tuckey URL rewrite filter to rewrite the query string passed to Solr.
Using the following configuration:
<urlrewrite use-query-strings="true">
<rule>
<from>^/test1/(.*)command=(.*)clean=true(.*)$</from>
<to type="redirect">$1command=$2clean=false$3</to>
</rule>
</urlrewrite>
This configuration works if the query string is not read as a query string i.e.
http://localhost/test1/command=something&clean=true&somethingelese=new
but fails as intended:
http://localhost/test1?command=something&clean=true&somethingelese=new
Any suggestions would be appreciated.
Thank you,
You need to include the question mark in the url.
Here is an example of how I've done it: https://github.com/KevinWorkman/StaticVoidGames/blob/master/StaticVoidGames/src/main/webapp/WEB-INF/urlrewrite.xml
But I think all you really want is something like this:
<urlrewrite use-query-strings="true">
<rule>
<from>^/test1/\?(.*)command=(.*)clean=true(.*)$</from>
<to type="redirect">$1command=$2clean=false$3</to>
</rule>
</urlrewrite>

IIS RewriteUrl RegularExpression

I'm using IIS 7.5 and UrlRewrite Module 2. I've read the official documentation for the module here, as well as many examples on SO and tested my regex on Regexpal. Somehow the translation into the Web.config file is causing me problems. I've tried escaping the parantheses also.
The regex I'm using is:
^partners/(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)
a few example urls:
partners/var/emea
partners/msp/apac/
partners/var/na/abcdabcdabcd
Here is the web.config I have so far:
<rules>
<rule name="partnersListing" enabled="true" stopProcessing="true">
<match url="^partners/(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)" />
<action type="Rewrite" url="partnersList.aspx?type=${R:1}&region={R:2}" />
</rule>
</rules>
Not sure if I want the action to be Rewrite or Redirect yet, but the pattern isn't working either way. Any help is greatly appreciated
Per your answer to my question in the comments:
Is this rule in the site root web.config or a subdirectory web.config?
#Kev in a subdirectory: "/partners"
From the docs:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference?amp;clcid=0x409
Quote:
Note that the input URL string passed to a distributed rule is always
relative to the location of the Web.config file where the rule is
defined. For example, if a request is made for
http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a
rewrite rule is defined in the /content directory, then the rule gets
this URL string default.aspx as an input.
Based on this you need to change your url match to:
^(var|distributors|msp|techpartners)/(na|emea|apac)(|/)(.*)
Assuming your re-write rule exists in:
http://example.com/partners/web.config
There is just one other thing which is what appears to be an extraneous $ character in your redirect/rewrite url in front of back reference {R:1}, I'm not sure if you intended that to be there or not.

URLrewriteFilter tuckey redirect

I am trying to use UrlRewriteFilter to do a redirect. As we are moving servers and infrastructure all the time to develop our in house cloud solutions we plan to use a lot of this.
My problem is I have a URL like
https:// google.com/web-app/servlet?request-parameters&values
I need to redirect all the URLs which has a value for a particular parameter to a different server.
so a URL like :
https:// google.com/administrator/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai
If the parameters contain cus=dubai, I need to redirect it to
https://hotmail.com/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai
I have tried the following but it doesn't seem to work
<urlrewrite use-query-string="true">
<rule match-type="wildcard">
<name>Redirect requests to cases</name>
<description></description>
<from>/administrator/servlet/**?**</from>
<to type="redirect" last="true">http://hotmail.com:8080/servlet/$1?&cus=dubai&$2&$3&amp</to>
</rule>
</urlrewrite>
Thanks for the help.
You will need to make use of the query-string and make sure it matches a particular condition.
From what I can see you would want something like this :
<condition name="query-string" operator="equal">*cus=dubai*</condition>
You will need a rule for each different parameter. One full rule would look like this :
<urlrewrite use-query-string="true">
<rule match-type="wildcard">
<name>Cus = dubai</name>
<from>/administrator/servlet/*</from>
<condition name="query-string" operator="equal">*cus=dubai*</condition>
<to type="redirect" last="true">https://hotmail.com/servlet/ej.processOrder?sec=100&geo=appointment&cus=dubai</to>
</rule>
<!-- other rules go here -->
</urlrewrite>
The condition shall be "type" instead of "name", as:
<condition type="query-string" operator="equal">*cus=dubai*</condition>
Good question and for a situation that I imagine occurs often it is not very well documented in the manual in my opinion.
The following worked for me -
<rule>
<condition type="query-string">foo=123&bar=456</condition>
<from>/index.jsp</from>
<to type="permanent-redirect" last="true">https://somehwere-else.com/blah/blah/</to>
</rule>
So this should work for you -
<rule>
<name>Redirect requests to cases</name>
<condition type="query-string">sec=100&geo=appointment&cus=dubai</condition>
<from>/administrator/servlet</from>
<to type="permanent-redirect" last="true">http://hotmail.com:8080/servlet/sec=100&geo=appointment&cus=dubai</to>
</rule>