Redirect URL in IIS 7.5 - url-rewriting

I want to all www domain to naked via IIS Rewrite engine. There are many domains pointing to same application.
Here is my rule :
^(www.)(.*)$
Action Type : Redirect
Redirect URL : {R:2}
Redirect Type : Permanent
When i test pattern for www.xxx.com
R:0 => www.stackoverflow.com R:1=> www. R:2 => www.stackoverflow.com
and that is fine.
What is wrong? And also should I include "http://" ?

Something like ^(www\.)(.+)$ should work when matching the http_host, but it might be better to specify the domain. From what I know of IIS (not much) and what it says on the net, something like:
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Oh, you said for any domain. If you want to make sure it ends in .com it should be something like
^(www\.)(.+)(\.com)$ against HTTP_HOST
.. oh, if you do that you need to back reference, so try something like this:
<rewrite>
<rules>
<rule name="Redirect www.domain.com to domain.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The c:1 is a back reference

Related

Web.config rewrite that excludes certain URL's needed

The standard web.config rewrite to turn all HTTP into HTTPS works fine
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="Temporary" />
</rule>
</rules>
</rewrite>
URLS that begin with HTTP://192.168 should not be rewritten but I've been unable to change the code to do this. I've tried to change the match to HTTP://192.168 and then negate it but I think that affects the action line

URL Rewrite rule in .NET to force www but ignore http(s)

I'm looking to set up a rewrite rule in my web.config file which forces URLs to use the 'www' sub-domain. This is done like so:
<rules>
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
However, the project is running multiple websites on multiple domains, some of which use SSL and some don't.
The code above hard-codes http:// into the redirect. What I'm looking to do is to abstract this such that the http or https protocols are maintained during the redirect without being hard-coded.
Some examples of the desired results are:
http://olddomain.com > http://www.newdomain.com
https://someolddomain.com > https://www.somenewdomain.com
Many thanks.
I found a reference which explains one approach in doing this. It uses rewrite key/value pairs as a 'variable' in the action url, as (as far as I can find out) there's no way currently to grab the protocol as a native variable inside the rule.
The rule has been modified as follows, which should do what's required:
<rewrite>
<rules>
<rule name="Add www maintain https" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="{MapSSL:{HTTPS}}www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapSSL" defaultValue="http://">
<add key="ON" value="https://" />
<add key="OFF" value="http://" />
</rewriteMap>
</rewriteMaps>
</rewrite>
There's also another reference which includes this amongst alternative ways to achieve the same thing.

IIS Rewriting url with parameter

I'm trying to setup rewrite for url from /place/search-the-places/item-detail/id=123 to /place/explore-our-places/item-detail/id=123
but it doesn't work, what I'm doing wrong?
<rewrite>
<rules>
<rule name="Collections" stopProcessing="true">
<match url="^place/search-the-places/item-detail/id=([0-9]+)" ignoreCase="true" />
<action type="Redirect" url="/place/explore-our-places/item-detail/?id={R:1}" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
Thanks!
If you just want to match numeric id as a parameter, then instead of
id=([0-9]+)/([_0-9a-z-]+)
use
id=([0-9]+)
because the slash between regexes means your route is like this
/place/search-the-places/item-detail/id=123/end-of-route
and this is not true, I suppose.
Also try using type="Rewrite" instead of "Redirect".

mvc 4 url rewrite is giving me a 404.4 even though the rewrite looks ok

I'm trying to get the url rewrite to change a request such as
http://foo.bar.com/
to
http://bar.com/foo/
I've popped in my URL rewrite rule to the web config like this
<rewrite>
<rules>
<rule name="SiteReWrite" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://bar.com/{C:1}/"/>
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
</conditions>
</rule>
</rules>
</rewrite>
but I get a 404.4 error. Looking at the Trace Log File I see this in there
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/>
<EventID>0</EventID>
<Version>1</Version>
<Level>4</Level>
<Opcode>42</Opcode>
<Keywords>0x0</Keywords>
<TimeCreated SystemTime="2012-12-03T05:54:01.237Z"/>
<Correlation ActivityID="{00000000-0000-0000-1000-0080030000FC}"/>
<Execution ProcessID="7312" ThreadID="3180"/>
<Computer>ULTRA</Computer>
</System>
<EventData>
<Data Name="ContextId">{00000000-0000-0000-1000-0080030000FC}</Data>
<Data Name="OldUrl">/</Data>
<Data Name="NewUrl">http://bar.com/foo/</Data>
</EventData>
<RenderingInfo Culture="en-US">
<Opcode>URL_CHANGED</Opcode>
</RenderingInfo>
<ExtendedTracingInfo xmlns="http://schemas.microsoft.com/win/2004/08/events/trace">
<EventGuid>{D42CF7EF-DE92-473E-8B6C-621EA663113A}</EventGuid>
</ExtendedTracingInfo>
</Event>
Which from the NewUrl looks like everything is peachy. Note, It looks like this instance was cached but I have seen the rule matches in the events previously.
If I enter the url http://bar.com/foo/ it works as it should.
I don't want the user to see the new url, they should think that they are on foo.bar.com not bar.com/foo. That's just for some routing I have in the mvc app.
Does anyone have any idea why it's doing this odd behavior? I'm using IIS express 8 for this, I haven't tried with IIS yet.
You should redirect and not rewrite:
<rewrite>
<rules>
<rule name="SiteReWrite" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://bar.com/{C:1}/"/>
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
</conditions>
</rule>
</rules>
</rewrite>
If you really want to rewrite you have to set up IIS as a reverse proxy by installing the ARR (Application Request Routing) module.
Since I didn't want the user to see the url change I couldn't use the redirect. However what I should have mentioned is that all subdomains are going to the same site. Once I'd thought about it a bit more, and with a bit more reading on the rewrite module I modified an MSDN example like this; which works like a charm
<rewrite>
<rules>
<rule name="Rewrite subdomain">
<match url="(.*)" /> <!-- rule back-reference is captured here -->
<conditions>
<add input="{HTTP_HOST}" pattern="^([^.]+)\.bar\.com$" /> <!-- condition back-reference is captured here -->
</conditions>
<action type="Rewrite" url="{C:1}/{R:1}" /> <!-- rewrite action uses back-references to condition and to rule when rewriting the url -->
</rule>
</rules>
</rewrite>

ISAPI PHP REWRITE

A friend is having me figure out a way to modrewrite on his windows server. He is running IIS 6 and Isapi is about the only thing I can find. I am not familiar with it and have read some of the documentation, but can't quite wrap my head around it. He is wanting to rewrite these URLS to make them clean
www.domain.com/cat.php?CTGID=####
and
www.domain.com/pp.php?ID=##
How would I go about rewriting these two URLS to make them Clean in ISAPI. I have installed it on the Windows Server and do I put these rules in IISF.ini in his website folder or put these codes in the master IISF file? Any Help would be greatly appreciated!
Rewrite rules go in a web.config file or in the ApplicationHost.config.
As described in http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/
Examples
<rewrite>
<rules>
<rule name="Force WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect from blog">
<match url="^blog/([_0-9a-z-]+)/([0-9]+)" />
<action type="Redirect" url="article/{R:2}/{R:1}" redirectType="Found" />
</rule>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
For IIS, if the ISAPI_Rewrite module is installed, $_SERVER["IIS_UrlRewriteModule"] is set and contains the version number of the module. So you can check for the existence of this server variable.

Resources