IIS URL Rewrite for Domains That Share Website - asp.net

I have two domains both pointing to the same IIS website
domain1.com/website1
domain2.com/website1
I would like to write a URL Rewrite rule to point
domain1.com/*
to a specific subdomain
domain2.com/subfolder.
I can't figure out the IIS URL rewrite rule to accomplish this. Below is what I have:
<rule name="redirect" enabled="true" >stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1\.com*$" />
</conditions>
<action type="Redirect" url="https://domain2.com/subfolder" />
</rule>

Try this syntax:
<rule name="redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1\.com*$" />
</conditions>
<action type="Redirect" url="https://domain2.com/subfolder" />
</rule>
i think there is a typo in your code, there is a ">" before stopProcessing that shouldnt be there.

Related

URLRewrite to add www for http and https

I need url-rewrite (or any other method) do the following:
http://domain.com > http://www.domain.com
https://domain.com > https://www.domain.com
I've seen a lot of posts to redirect to www and a lot to force https but none of them that does both.
Thanks
this should help:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.+)://" />
<add input="{HTTP_HOST}" pattern="^domain\.com$" />
</conditions>
<action type="Redirect" url="{C:1}://www.domain.com/{R:1}" />
</rule>
this rule i use is applying WWW for both http and https (just copy and paste) :
<rule name="ensure www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
hope this helps.

Redirect only when no subdirectories are selected

I'm trying to to redirect on xyz.sample.com to www.sample.com/order/xyz, which works fine, but the problem is now that resources like xyz.sample.com/js/file.js redirects to www.sample.com/order/xyz/js/file.js which is no right, how can i only do the redirection when the url is just xyz.sample.com and im not trying to access anything inside, below is my rewrite rule:
<rewrite>
<rules>
<rule name="Rewrite service provider to web ordering page" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*)\.sample\.com$" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^manage\.sample\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="order/{C:1}" />
</rule>
</rules>
</rewrite>
Thanks.

ASP.Net Redirect rule for all except certain URL

I have the following redirect rules one is for non www url (http://mysite.co.uk/....) and the other is for a www URL to redirect to http://www.mysite2.co.uk/...
Currently the rules are a catch all. I don't want the rules to execute if a certain URL is hit which contains "/mystring/mystring.aspx".
Can anyone help me write the rules for this?
<rule name="Canonical Host Name - mysite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www|[^.]+\.)*mysite\.co.uk$" />
</conditions>
<action type="Redirect" url="http://www.mysite2.co.uk/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host - mysite 2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.co.uk$" />
</conditions>
<action type="Redirect" url="http://www.mysite2.co.uk/{R:1}" redirectType="Permanent" />
</rule>
You should be able to accomplish this by adding to the conditions, and using negate="true". Something like this:
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(?:www|[^.]+\.)*mysite\.co.uk$" />
<add input="{PATH_INFO}" pattern="mystring\/mystring\.aspx" negate="true" />
</conditions>

remove .aspx extention using IIS7 & URL Rewrite [duplicate]

I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Then i have this inside <system.webServer>.
<rewrite>
<rules>
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Now i want to remove all the .aspx in the end of my pages. Example:
http://www.example.com/Register.aspx
Will turn into:
http://www.example.com/Register/
How can i do that?
I'm on Shared Web Hosting on GoDaddy using IIS7.
These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx" />
</rule>
<rule name="Rewrite page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:
<rule name="Rewrite Product ID" stopProcessing="true">
<match url="^product/([0-9]+)$" ignoreCase="false"/>
<action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>
If you want to use lower and upper case letters in the URL, set ignoreCase="true"
Edit to answer your second question plus a bonus
This rule will redirect aspx page to the clean URL:
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.
Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:
if (!String.IsNullOrEmpty(Request.RawUrl))
form1.Action = Request.RawUrl;
or for IIS 7:
if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.
First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
<action type="Rewrite" value="www.myserver.com/{R:1}" />
</rule>
</outboundRules>
</rewrite>
this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages

redirect example.com to www.example.com with IIS7

I've gone through many suggestions here and on the web, and still am failing to make this work. I currently have the following, but it's not working. All help appreciated!
<rules>
<rule name="www-less redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
Your rule seems fie. Try this one (slightly different) -- works fine for me:
<rule name="CanonicalHostName">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
1) Try moving this rule to the top (make it first rule).
2) Possibly (just possibly) you do not have binding for example.com, only for www.example.com ?

Resources