I've got the following url:
inspiratie/color-collections/test/test/S%200502-B/test
And the rewrite rule within the web.config:
<rule name="color2">
<match url="(kleur-en-inspiratie|inspiratie)/color-collections/([\S ^/]+)/([\S ^/]+)/([\S ^/]+)(/)?([\S ^/]*)" />
<action type="Rewrite" logRewrittenUrl="true" appendQueryString="false" url="{R:1}/color-collections/color-detail?colorcollection={R:2}&colorfamily={R:3}&color={R:4}&colorname={R:6}" />
</rule>
After the rule kicks in, the url becomes:
inspiratie/color-collections?colorcollection=test&colorfamily=test&color=S&colorname=
But the expected output would be:
inspiratie/color-collections?colorcollection=test&colorfamily=test&color=S 0502-B&colorname=test
As you can see if the space is used within the color code, the query string is broken after the color param ({R:4}).
When I test the rule within IIS, it works fine and returns the expected results.
I know that the unescaped white space will not work, but expecting the escaped one to work.
In your case problem cause because of encoding. You need to use {UNENCODED_URL} variable for regexp. Rule should be like that:
<rule name="color2">
<match url=".*" />
<conditions>
<add input="{UNENCODED_URL}" pattern="(kleur-en-inspiratie|inspiratie)/color-collections/([\S ^/]+)/([\S ^/]+)/([\S ^/]+)(/)?([\S ^/]*)" />
</conditions>
<action type="Rewrite" logRewrittenUrl="true" appendQueryString="false" url="{C:1}/color-collections/color-detail?colorcollection={C:2}&colorfamily={C:3}&color={C:4}&colorname={C:6}" />
</rule>
Related
I get 500 error when having this rule:
<rule name="Remove Query String" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^url=[^&]+" />
</conditions>
<action type="Redirect" url="{C:1}webp" appendQueryString="false" />
Problem is inside
<add input="{QUERY_STRING}" pattern="^url=[^&]+" />,
actually pattern seems wrong but works corrently when I check it online. All parsers parse it.
What I want to achieve is to redirect all URL that have query string url= to url whose value corresponds to the value of url in initial request but ignroeing everything after &, or everything after something else (like webp for example) That is reason I want to separate pattern in multiple logical groups.
I have a help section on a web site, which for now I want it to redirect to another place. The rest of the site should stay the same, which means that only the help section/content should make the user go to another site:
device.domain.net/collection/hgkhghj should return to device.domain.net/collection
I can have anything at the place of hgkhghj . If i am writing device.domain.net/collection then it should return device.domain.net/collection
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)/collection(.*)" ignoreCase="true" />
<action type="Redirect" url="http:/device.domain.net" appendQueryString="false" redirectType="Permanent" />
</rule>
but currently it is returning to device.domain.net.
I want to make one more Rule in which if i enter device.domain.net/gcfhgg then it should return to device.domain.net.
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<action type="Redirect" url="http://device.domain.net" appendQueryString="false" redirectType="Permanent" />
</rule>
But it is not working.
For your first rule you could use the following (assuming it is on the same domain):
<rule name="Help Redirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^collection/(.*)" />
<action type="Redirect" url="collection" appendQueryString="false" redirectType="Permanent" />
</rule>
If this is on another domain you will need ARR installed as well. Your second rule is more complicated as it looks as if you are attempting to perform a redirect only if you hit a 404 page. This is something I have never done using rewrite rules. Is there a reason for doing this as opposed to showing a custom 404 page?
I have been asked to set up a redirect from:
https://www.#####.co.uk/news/uk-families-have-more-money%E2%80%93butalso-more-debts/
TO
https://www.#####.co.uk/news/
I have added the following rule to my web config:
<rule name="Redirect" stopProcessing="true">
<match url="news/uk-families-have-more-money%E2%80%93butalso-more-debts/" />
<action type="Redirect" url="http://#######.co.uk/news/" redirectType="Permanent" />
</rule>
But it is not having any effect.
I know '%E2%80%93' for UTF8 represents '-', so I have tried substituting for that, but the rule is still not having an effect.
Any ideas? Is there anyway I can debug this?
I'm trying to create a rewrite rule such as:
~/company_search.aspx?cat=987&page=2&loc=1234
to
~/company_category/987/estiatoria/?loc=1234&page=2
My rewrite rule is:
<rule name="Company Category">
<match url="company_category/([0-9]+)/(?:[^/]*)/?" />
<action type="Rewrite" url="company_search.aspx?cat={R:1}" appendQueryString="true" />
</rule>
The part /estiatoria/ in the sample rewritten url is optional for seo optimization and not specified in the actual querystring neither has any other use. When I do a postback I get the cat parameter 2 times with a value 987,987 and my server-side code gets messed up.
So I tried to manually append the querystring in order to exclude the cat parameter and append all the others. So I changed the rule to:
<rule name="Company Category">
<match url="company_category/([0-9]+)/(?:[^/]*)/?(?:\?|&)?(.*)" />
<action type="Rewrite" url="company_search.aspx?cat={R:1}&{R:2}" appendQueryString="false" />
</rule>
But I don't get any other values in the querystring except cat. Am I doing something wrong?
Update:
I think I must add a querystring condition in order to exclude the cat parameter and then append the rest matching result. So for the querystring:
?cat=987&page=2&loc=1234
I'd like to get:
?page=2&loc=1234
and the cat parameter could be in any order or not present.
I tried:
<rule name="Company Categories Search">
<match url="company_category/([0-9]+)/(?:[^/]*)/?" />
<conditions>
<add input="{QUERY_STRING}" pattern="(?!cat=[0-9]+)(.*)(cat=[0-9]+|$)(?!cat=[0-9]+)(.*)" />
</conditions>
<action type="Rewrite" url="company_search.aspx?cat={R:1}&{C:1}&{C:3}" appendQueryString="false" />
</rule>
When I hit the url:
~/company_category/987/estiatoria/?page=2&loc=1234
I get on the server side Request.Querystring the following value:
cat=987&page=2&loc=1234&
but when I do a postback I get:
cat=987&at=987&page=2&loc=1234&
Can someone please help me with that last regex condition?
Update 2:
I've come to a solution but it doesn't cover every scenario:
<rule name="Company Categories Search">
<match url="company_category/([0-9]+)/(?:[^/]*)/?" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern="(cat=[0-9]+)(.*)" />
<add input="{QUERY_STRING}" pattern="(^)(.*)" />
</conditions>
<action type="Rewrite" url="company_search.aspx?cat={R:1}&{C:2}" appendQueryString="false" />
</rule>
I've used 2 condition rules with MatchAny option in order to match one: the query string when the cat value is present in the mapped url (currently only in postbacks) and two: when it's not present.
If the cat parameter is somehow between page and loc parameters the previous parameter is not included in the query string. If anyone has a better solution please let me know.
I have to following URL:
http://example.org/sessionstuff/kees/view.aspx?contentid=4&itemid=5
It needs to be rewritten so it will go to:
http://example.org/sessionstuff/view.aspx?site=kees&contentid=4&itemid=5
Basically it will take the kees value and put it as a site parameter. I'm using the IIS URL Rewrite module that uses rules in my web.config. I've added the following code to my web.config:
<rule name="RedirectSite" stopProcessing="true">
<match url="^(\D[^/]*)/(.*)$" />
<action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>
Everything works fine, but when I do a postback, the site parameter is doubled. I've tested this by using the following code on my .aspx page:
<h3>Querystring</h3>
<ul>
<% foreach (string key in Request.QueryString.Keys)
Response.Write(String.Format("<li><label>{0}:</label>{1}</li>", key, Request.QueryString[key])); %>
</ul>
<asp:Button runat="server" Text="Postback" />
First time
Querystring
site: kees
contentid: 4
itemid: 5
Second time
Querystring
site: kees, kees <--- double
contentid: 4
itemid: 5
How to prevent the site parameter from duplicating itself? Each postback will add another value.
Note: the other query parameters must be present, so using appendQueryString="false" seems not an option.
It looks like this could be solved by not rewriting the URL if it already contains the site= parameter (regardless of where in the query string it's located). So how do we do that?
Check out number 9 here: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
I don't have a way to test this now, but I reckon something like this should work:
<rule name="Query String Rewrite">
<match url="^(\D[^/]*)/(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^((?!site=).)*$" />
</conditions>
<action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>
NOTE: I am not great at rule rewriting, but this regex ^((?!site=).)*$ matches when the string DOES NOT contain site= and this is when you want your rewrite rule to operate, hence me adding that as a condition. I reckon you may be able to do this more efficiently.
What I am trying to do here is say: Let's rewrite the rule, but only if it doesn't already contain the site parameter.
I hope this is enough for you to answer the question!
===
This seems to work:
<rule name="RedirectSite" stopProcessing="true">
<match url="^(\D[^/]*)/(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^((?!site=).)*$" />
</conditions>
<action type="Rewrite" url="{R:2}?site={R:1}" />
</rule>
<rule name="RedirectSite2" stopProcessing="true">
<match url="^(\D[^/]*)/(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="site=" />
</conditions>
<action type="Rewrite" url="{R:2}" />
</rule>