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.
Related
My goal is to redirect all www.* urls to non-www urls. For example:
If the url is www.mydomain.com/users it should redirect to mydomain.com/users.
In order to achieve that I have written the following code in my web.config:
<rule name="Redirect www.* urls to non www" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www$" />
</conditions>
<action type="Redirect" url="{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
</rule>
but it does nothing and I can see the www urls not redirecting to non www urls.
Can you share what I am doing wrong there?
Do note that I don't want to add any hard coded domain in that rule. I want to make it generic.
I need a generic solution where in my rule there is no where a hard coded domain and a hard coded protocol is present.
Well, here is the solution I came up with.
I have provided the solution with all the details along with comments comments for the regex, capturing groups etc. used in the rule:
<rule name="Redirect www.* urls to non www" enabled="true">
<!--Match all urls-->
<match url="(.*)"/>
<!--We will be capturing two groups from the below conditions.
One will be domain name (foo.com) and the other will be the protocol (http|https)-->
<!--trackAllCaptures added for tracking Capture Groups across all conditions-->
<conditions trackAllCaptures="true">
<!-- Capture the host.
The first group {C:1} will be captured inside parentheses of ^www\.(.+)$ condition,
It will capture the domain name, example: foo.com. -->
<add input="{HTTP_HOST}" negate="false" pattern="^www\.(.+)$"/>
<!-- Capture the protocol.
The second group {C:2} will be captured inside parentheses of ^(.+):// condition.
It will capture protocol, i.e http or https. -->
<add input="{CACHE_URL}" pattern="^(.+)://" />
</conditions>
<!-- Redirect the url too {C:2}://{C:1}{REQUEST_URI}.
{C:2} captured group will have the protocol and
{C:1} captured group will have the domain name.
"appendQueryString" is set to false because "REQUEST_URI" already contains the orignal url along with the querystring.
redirectType="Permanent" is added so as to make a 301 redirect. -->
<action type="Redirect" url="{C:2}://{C:1}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent"/>
</rule>
It will do the following redirects:
http://www.foo.com -> http://foo.com
https://www.foo.com -> https://foo.com
http://www.foo.com?a=1 -> http://foo.com?a=1
https://www.foo.com?a=1 -> https://foo.com?a=1
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>
I want to add some URL rewriting stuff in my web.config
The source URL:
http://constant.com/caam/verifying/?token=kpG1TwYo2KqTS%2bKg%2fY6lVm2Gt
Need to convert it to URL:
http://constant.com/caam/verifying/default.aspx?token=kpG1TwYo2KqTS%2bKg%2fY6lVm2Gt
Any ideas on how to accomplish this or other suggestions much appreciated it.
A basic redirect rule in IIS (web.config) might look like:
<rule name="Token Redirect" stopProcessing="true">
<match url="caam/verifying.*" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="&?(token=[^&]+)&?" />
<add input="{REQUEST_URI}" pattern="default.aspx" negate="true" />
</conditions>
<action type="Redirect" url="/caam/verifying/default.aspx?{C:1}" appendQueryString="false" redirectType="Found" />
</rule>
You can change the match url, but basically this is matching everything that starts caam/verifying.
It then (additionally) checks that the query string has "token=" in it somewhere, and captures its value (it will go into the capture 1 as there's nothing else here, eg {C:1}).
We then output the redirect as /caam/verifying/default.aspx?{C:1} (where {C:1} is "token=12345", for example).
Note that this rule will only get hit if the URL matches (the caam/verifying part) and the parameters match (there's the "token=" part) - this redirect rule gets skipped otherwise.
EDIT
I've added an additional "negate" rule to not match against the "default.aspx" page.
I'm trying to work in a rewrite problem, but its my first time and I guess I'm doing something very wrong.
A few users of our portal, try to navigate typing https://www.dudalina.proveagora.com instead the right one https://dudalina.proveagora.com. We have the SSL only for *.proveagora.com, so I started to try a redirect on the users who type the wrong url.
Here, the print of rewrite working great:
but, some users type https:// and some users type http://. By this way, I've tried to change the pattern.
I dont know why, but using the second pattern, the url https://www.dudalina.proveagora.com doesnt redirect anymore.
Just to explain a little more, the dudalina part is dynamic too. Dudalina is the store name, and it will change accordingly with store. So, we can have latter something like http://bestbuy.proveagora.com or anything else and this patterns much works with all the options.
I know, the user cant type www.bestbuy.proveagora.com but they do, and we need to make it works! :(
Any help?
EDIT
xml Web.Config
<rewrite>
<rules>
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(https\:\/\/|https\:\/\/)?([a-zA-Z]+\.)?(proveagora\.com){1}(\/.*)?$" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="http://dudalina.proveagora.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Here you go:
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.+\.)proveagora\.com$" />
<add input="{HTTP_HOST}" pattern="^dudalina\.proveagora\.com$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="https://dudalina.proveagora.com" appendQueryString="false" />
</rule>
What it will check is that the protocol used is HTTPS and that the subdomain of proveagora.com is present and different from dudalina.
If that's the case, it redirects to https://dudalina.proveagora.com
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.