i am trying to rewrite url for home page and category in webconfig c# - iis

<rule name="Default" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{URL}" negate="true" pattern="\." ignoreCase="false"/>
</conditions>
<action type="Rewrite" url="Default.aspx?Alias={R:1}"/>
</rule>
<rule name="Categorys" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{URL}" negate="true" pattern="\." ignoreCase="false"/>
</conditions>
<action type="Rewrite" url="/Vn/Categorys.aspx?Alias={R:1}"/>
</rule>
i want URL:
https://domain/ ==> Default
https://domain/Categorys ==> Categorys
but it only goes to the homepage
can you help me fix it?

From the above description, it looks like your requirement is to redirect from https://domain/Category to https://domain/Vn/Categorys.aspx?Alias=123
I could see that your rules does not has correct patterns.
I would suggest you try to make a test with the rule below.
<rewrite>
<rules>
<rule name="rule-4" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="/Vn/Categorys.aspx?Alias={C:1}" appendQueryString="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="/category/(.*)" />
</conditions>
</rule>
</rules>
</rewrite>
Output:
Further, you could modify the rule as per your own requirements.

Related

iis rewrite url for https://example.com/default.aspx/default.aspx to https://example.com

I have tried all kinds of rewrite URL's and can't get IIS to send the URL of https://example.com/default.aspx/default.aspx to https://example.com/default.aspx
Google has indexed the site with the wrong URL when Bing got it right (go figure). Any help would be much appreciated. All my traffic is going to the wrong url (https://example.com/default.aspx/default.aspx).
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^https://example.com/default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>
<rule name="redirect two character to default" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/[a-z]{2}(/)?$" />
</conditions>
<action type="Redirect" url="default.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
There is some issue in your rule. {HTTP_HOST} only matches the hostname which is www.example.com it will not match the whole URL.
You could try below rule:
<rule name="Redirect www.xxx.com to xxx.com" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="default/default|default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>

IIS URL Redirect change querystring parameter name

I am trying to redirect from one site to another like the following.
http://example.com/quickscan/?code=123456
To
https://example2.com/as-quickscan/login?username=123456
The domains are different. Also the redirect URL parameter and URL structure is entirely different.
What I have tried is the following and some other variations.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/\?[a-zA-Z]+=([0-9]+)?$" ignoreCase="true" />
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" appendQueryString="false"/>
</rule>
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="^quickscan/\?[a-zA-Z]+?=([0-9]+)?$" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={R:1}" redirectType="Permanent" appendQueryString="false" />
</rule>
This is fixed. I used the following.
<rule name="Redirect quickscan" stopProcessing="true">
<match url="^quickscan/" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="([a-z]+)=([0-9]+)" />
</conditions>
<action type="Redirect" url="https://example2.com/as-quickscan/login?username={C:2}" appendQueryString="false" />
</rule>

URL Rewrite to subdomain

For a URL rewrite I need support.mydomain.com to point to mydomain.com/support. What I have is rewriting to support.mydomain.com/support.
I've tried 2 different variations, but no luck. Any help is greatly appreciated.
Version 1
<rewrite>
<rules>
<rule name="rewrite support" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^support.mydomain.com$" />
</conditions>
<action type="Rewrite" url="\support\{R:0}" />
</rule>
</rules>
</rewrite>
Version 2
<rule name="Support Rule" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(\w+)\.mydomain\.com$" />
</conditions>
<action type="Rewrite" url="{C:1}/{R:1}" />
</rule>
I was able to find this post by Scott Forsyth that got this working for me.
As he states in the article "This will redirect the link http://anything_except_www.domain.com to http://domain.com/anything_except_www."
<rewrite>
<rules>
<rule name="CName to URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://domain.com/{C:1}/" />
</rule>
</rules>
</rewrite>
Hope this helps someone!

IIS URL rewriting fails

I'm trying to implement simple IIS URL rewrite. I want to rewrite URL: about.test.local to text.local/about.html .
In configuration below I have added rules, but it doesn't seem to work. What is the problem?
<rewrite>
<rules>
<rule name="Rewrite1" patternSyntax="ExactMatch" stopProcessing="true">
<match url="about.test.local" />
<action type="Rewrite" url="http://test.local/about.html" />
</rule>
<rule name="Rewrite2" patternSyntax="ExactMatch" stopProcessing="true">
<match url="http://about.test.local/" />
<action type="Rewrite" url="http://test.local/about.html" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
As #beavel mentioned, I have added following rules, but URL still doesn't get rewritten:
<rule name="Rewrite2a" patternSyntax="ExactMatch" stopProcessing="true">
<match url="(.+)" />
<conditions>
<add input="{HTTP_HOST}" pattern="about.test.local" />
</conditions>
<action type="Rewrite" url="http://test.local/about.html" />
</rule>
<rule name="Rewrite2b" patternSyntax="ExactMatch" stopProcessing="true">
<match url="(.+)" />
<conditions>
<add input="{HTTP_HOST}" pattern="http://about.test.local/" />
</conditions>
<action type="Rewrite" url="http://test.local/about.html" />
</rule>
The problem is that the match url doesn't contain the host as explained here. If you have multiple hostnames bound to one IIS site and only want to rewrite the one hostname you would do something like:
<rule name="Rewrite1" patternSyntax="ExactMatch" stopProcessing="true">
<match url="(.+)" />
<conditions>
<add input="{HTTP_HOST}" pattern="about.test.local" />
</conditions>
<action type="Rewrite" url="http://test.local/about.html" />
</rule>

Multiple URL Rewrite rules getting conflicted

I have the following situation with url rewrite rules which are getting conflicted with each other:
Rule 1: I need to redirect my domain to https
Rule 2: I need to redirect www.mydomain.com --> https://mydomain.com
Rule 3: I need both www.mydomain.com and mydomain.com to redirect to https://mydomain.com/myfolder, but if I have mydomain.com/mysecondfolder should only redirct to https://mydomain.com/mysecondfolder
what I was able to achieve is everything but redirecting www.mydomain.com to https://mydomain.com (just because it is being conflicted with another rules, if alone it is working).
My rules are:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to myfolder" enabled="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/myfolder" />
</rule>
I was able to solve this using the below rules:
<rewrite>
<rules>
<rule name="Canonical Host Name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^myapp\.com$" />
</conditions>
<action type="Redirect" url="http://myapp.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to items" enabled="false">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/items" />
</rule>
</rules>
</rewrite>

Resources