url rewriting rule in asp.net web.config - asp.net

I am using URL rewriting like the following .
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But I want another rule that when users write www.mysite.com , they go redirected automatically to www.mysite.com/home , i can do that in url routing but it'll take extra phases so I want it in url rewriting...can someone write it for me please?? Thanks alot.

Related

Http to Https URL rewriting

My requirement is straight forward. i have Http://abc need to make it Https://abc . I have added the folowing code in web.config. i.e. added new rule in IIS.
I have folowwed the URL rewriting module in IIS.
<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}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
But still it doesn't work for me. Help me out.
The code you are using is very similar to what I use in production except that I use redirectType="Permanent" and I happen to use a hard coded domain name to ensure a canonical domain and I use {R:1} for the path and query but I think using {HTTP_HOST}{REQUEST_URI} as you are should work however you may need a slash between the two.
Give this a try:
<system.webServer>
<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="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

web.config Redirect Rule - Match URL

This seems to be a pretty straight forward change, however it's not matching the URL and none of the answers on stackoverflow seem to address this simple use of redirect rules. I believe it has something to do with the '?id=XXXX' portion of the URL.
We have some old versions of pages, which I am trying to add redirects to the new version of the pages.
Here's an example of my rules:
<rule name="old_Page" stopProcessing="true">
<match url="^Page.aspx?id=12345"/>
<action type="Redirect" url="http://www.example.com/newPage.aspx" redirectType="Permanent" />
</rule>
Any help would be most appreciated.
I used a different method to create Static Redirects in the web.config from this article:
https://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
<rewrites>
<rules>
<rule name="Redirect Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.example.com{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="Page.aspx?id=12345" value="/newPage.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrites>
I can add as many redirects as I like in the rewrite map section by adding additional keys.

Creating 301 redirects from asp.net website to another

I have a website which i'm planning to close down and replace with another with a different domain, how can i go about setting up 301 redirects from specific pages on my old site to the relevant pages on my new one?
I've had a look into the IIS Url Rewrite Module, but wasn't able to figure it out. Help please?
I've tried this
<rewrite>
<rules>
<rule name="Redirects to new domain" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="http://domain.com/" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
But nothing happens when navigation to the homepage
You can update your web.config with routing rules like this:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirects to new domain" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="olddomain.net" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

How can I redirect sub-domain to primary domain?

I have a scenario where I have two domains pointing to same server one is the primary domain to all sub-sites and then there is secondary domain for each sub-site so what I am trying to do is redirecting the secondary domain to primary domain using web.config file.
For Example: if some one enters www.domain-secondary.com it should redirect to www.domain.com/subsite
update: As per some answers on this question and with some amendments i managed to made it work but apparently it only works if domain doesn't contains any special characters such as æ,ø,å. how can i resolve this issue? Please help
Non-working code which contains special characters
<rule name="Redirect to WWW rule 2" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.skivebøligmøntering\.dk$" />
</conditions>
<action type="Redirect" url="http://www.skive.dk/skive-boligmontering/{R:1}" redirectType="Permanent" />
</rule>
Try this one .
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to 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>
</rules>
</rewrite>
</system.webServer>
</configuration>

Microsoft rewriting module - Force www on URL Or remove www from URL

I have a shared hosting plan with Windows Server 2008 and IIS7.5, and there is Microsoft rewriting module installed and enabled.
<rewrite>
<rules>
<rule name="myRule" patternSyntax="Wildcard">
<!--Rewriting code-->
</rule>
</rules>
</rewrite>
So, how to redirect mydomain.example/everywhere-in-site/my-page.html to www.mydomain.example/everywhere-in-site/my-page.html with Microsoft rewriting module?
And what if I want to redirect www.mydomain.example/everywhere-in-site/my-page.html to mydomain.example/everywhere-in-site/my-page.html?
To remove the www from a domain and redirect to a "naked domain" you could di it like in the following code snippet:
<rewrite>
<rules>
<rule name="Remove WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yourdomain\.example$" />
</conditions>
<action type="Redirect" url="http://yourdomain.example/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
And the other way around (if you prefer that) to redirect a non-www to one with www:
<rewrite>
<rules>
<rule name="Add WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain\.example$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.example/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The redirectType="Permanent" is of course optional but for SEO and most scenarios I would recommend it.
Please see also these Stack Overflow questions/answers:
IIS7 URL Rewrite - Add "www" prefix
Forwarding http://mydomain.example/ctrlr/act/val to http://WWW.mydomain.example/ctrlr/act/val
Proper method to remove www from address using IIS URL Rewrite

Resources