URL rewriting with space & "+" symbol in web.config - asp.net

I want to redirect my old url
"http://staging.TempSite.com/about-us/Institute-+target/%20"
to new url ""http://staging.TempSite.com/about-us/news-events""
<rule name="rule59" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="staging.TempSite.com/about-us/Institute-\+target/\s*" />
</conditions>
<action type="Redirect" url="./about-us/news-events" />
i also tried followoing patterns.
pattern="staging.TempSite.com/about-us/Institute-\+target/\s*$"
pattern="staging.TempSite.com/about-us/Institute-\+target/ $"

#Wiktor Stribiżew comment is almost correct, but it will not work. If you have + in your URL, you need to enable allowDoubleEscaping in your config. Example of config:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
<rewrite>
<rules>
<rule name="rule59" stopProcessing="true">
<match url="^about-us/Institute\-\+target/\s*$" />
<action type="Redirect" url="/about-us/news-events" />
</rule>
</rules>
</rewrite>
</system.webServer>

Related

How to block all other referer except localhost in web.confg

I have added a Rule to prevent referer spam in my web.config. but i want to allow only localhost and block all other referers in the same. Here is what i am trying but it is not working.
enter code here
<system.webServer>
<rewrite>
<rules>
<rule name="abort referer spam requests" stopProcessing="false">
<match url="^localhost:49363$" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^localhost:49363$" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please try this rule.
This rule will just allow null reference or localhost and block request referenced from other domain.
<rule name="abort rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(^$|localhost)" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>

URL Rewrite to a subdomain

I am using the Rewrite tool for ASP.NET to redirect from http to hpps. I want to reroute to
https://services.net/ExitInterview/home/about
But currently it is routing to
https://services.net/home/about
Below is my redirect rule:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="Permanent" />
</rule>`
Can I mix the "HTTP_HOST" text with hard-coded text in the rule string? Or is there another way?
I don't want to hard code the url because it changes with local, staging, and production.
<rule name="HTTP to HTTPS redirect" ="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/ExitInterview/{R:1}"
redirectType="Permanent" />
</rule>
Give this a try
This should do what you want in terms of redirecting to HTTPS from HTTP in a web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<httpRuntime executionTimeout="180" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="PassThrough" />
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
I use this exact snippet for a webserver that enforces HTTPS redirection, but that also seems to be pretty close to what you have. Are you sure you have configured the structure of the web.config file correctly? - I remember running into issues when I would leave out something.

WebConfig URL Rewrites in IIS

Have a ASP.NET solution and want to achieve 2 things: -
Redirect www.mydomain.com to mydomain.com
Have friendly URLs
So ... here is what I have added the system.webserver section of the webconfig...
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="homepage" stopProcessing="true" >
<match url="^/wwwzone/homepage.aspx$"/>
<action type="Redirect" url="/"/>
</rule>
<rule name="homepageReal" stopProcessing="true" >
<match url="^/$"/>
<action type="Rewrite" url="/somepath/homepage.aspx"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https"/>
<add key="off" value="http"/>
</rewriteMap>
</rewriteMaps>
</rewrite>
This fails. The logic behind this is: -
First rule takes care of redirecting www.mydomain.com to mydomain.com. This works. and with the rewriteMap it also handles HTTP/HTTPS correctly.
The second rule is supposed to do a browser redirect if they request an unfriendly (real) URL.
The last one is designed to convert the friendly URL back to the real URL, however this is a rewrite not a redirect.
Any thoughts much appreciated.
It turns our was poor syntax in my regular expressions.
So, matching the real URL of the home page should have been
<match url="^$"/>
not
<match url="^/$"/>
And the subsequent rule to rewrite to the real URL should change in the same way.
So, for completeness, this one works...
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="homepage" stopProcessing="true" >
<match url="^wwwzone/homepage.aspx"/>
<action type="Redirect" url="/"/>
</rule>
<rule name="homepageReal" stopProcessing="true" >
<match url="^$"/>
<action type="Rewrite" url="/somepath/homepage.aspx"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https"/>
<add key="off" value="http"/>
</rewriteMap>
</rewriteMaps>
</rewrite>

Proper method to remove www from address using IIS URL Rewrite

What is the optimal way to remove the www subdomain from a url using IIS URL Rewrite?
If you want it to work with any hostname (not hardcoding it into the rule), you'd want to do something like this:
<rule name="Remove www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
in the redirect action, the {C:1} contains the second capturing group in the condition, whereas the {R:0} contains whatever was in the rule (the path). appendQueryString="true" will also append any querystring to the redirect (if present). Keep in mind though, that any url hashes, if present, will be lost in the process since those don't get passed to the server.
IIS does it automatically for you:
Select site > URL rewrite > new rule > Canonical Host Name :)
The following one should work :
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
To do a redirect that will work for both http and https the following can be used
<rewrite>
<rules>
<rule name="Lose the www" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.*)$"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="{SchemeMap:{HTTPS}}://{C:1}/{R:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="SchemeMap">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Force Https on an ASP.NET app running on IIS 7.5

I am forcing SSL on my entire site with the following code on my web.config file;
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
</system.webServer>
but what I would like to do is to force ssl only the ~/purchase/ and ~/account/ path and under them. what should be the match url for that?
NOTE Regular Expressions also would work for me here as well as wildcard.
You should use this pattern (this will work for /purchase/something as well as /account/something-else):
^((purchase|account)/.*)$
You have to remember, that URL should have no leading slash /.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS" stopProcessing="true">
<match url="^((purchase|account)/.*)$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
you would do something like this (2 separate rules for simplicity)
<match url="/purchase/(.*)"/>

Resources