Configure redirect asp.net/iis7 - asp.net

I have a website that opens when the user enters either http://example.com or http://www.example.com.
I need to configure the server or the app to redirect with a permanent redirect to www.example.com when example.com is accessed.
What is very important is that the path is preserved, so if example.com/path/page.aspx?p=1, the redirect should be done to www.example.com/path/page.aspx?p=1.
thanks!

Using the URL Rewrite you can do this by adding a configuration in you web.config. You need to install this module in your IIS as well. Here is an example, not fully tested:
<system.webserver>
<rewrite>
<rules>
<rule name="Redirecting" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP}" pattern="^(http://)?example.com" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>

The URL Rewrite module should do exactly what you need.

Related

IIS 10 site needing URL Rewrite for changed WordPress subfolder name

I have a client with an ASP.NET MVC project running on IIS 10.
In IIS 10 I have an Application setup under the site that points to a WordPress installation folder. The alias was previously oldblog and has now changed to blog.
The WordPress URI was requested to be changed so I need to permanently redirect requests to the old URI to the new one.
Root ASP site: https://www.example.com/
Old WP URI: https://www.example.com/oldblog/
New WP URI: https://www.example.com/blog/
The following rule only partially works. oldblog does get rewritten to blog, but the rest of the URI is unpredictable/inconsistent.
<rule name="Rewrite /oldblog to /blog" stopProcessing="true">
<match url="^oldblog(.*)$" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/blog/{R:1}" redirectType="Permanent" />
</rule>
I need to rewrite all requests to /oldblog/* to /blog/* - regardless of the full URI/querystring, I only want to rewrite oldblog to blog
/oldblog/ => /blog/
/oldblog/category/somecategory/ => /blog/category/somecategory/
I would suggest you try to make tests with the URL Rewrite rule below could help you redirect from URLs below.
/oldblog/ => /blog/
/oldblog/category/somecategory/ => /blog/category/somecategory/
Sample Rule
<rewrite>
<rules>
<clear />
<rule name="Rule-3" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{REQUEST_URI}" pattern="/oldblog(.*)|/oldblog" />
</conditions>
<action type="Redirect" url="/blog{C:1}" />
</rule>
</rules>
</rewrite>
Test Result
Further, you could modify the rule above as per your own requirements.

Redirect non WWW site to https:// IIS

We have a non WWW website that we want to redirect to https://
The site is being hosted on IIS and we were looking to just type websitename.domain.com and have it redirect to https:// We have a URL rewrite rule in place but it when typing in the websitename.domain.com, it won't even try to go to https:// or http:// Is there something else we have to set for this to work?
Thanks.
First, make sure you bind the site bindings with websitename.domain.com for HTTP and https protocol like below:
Try below rule to redirect http to https:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS force" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

ASP.net URL Rewrite subdirectory to external URL

I need to URL Rewrite a subdirectory to an external domain.
Example: When visiting "https://example1.com/test", "https://example2.com/hello" should open. The URL should still be "https://example1.com/test".
I tried to solve this by adding a Rewrite rule in web.config, but it don't works. Here is the Rewrite rule I made:
<rule name="TestRule" stopProcessing="true">
<match url="^test(/.*)?$" />
<action type="Rewrite" url="https://example2.com/hello" appendQueryString="false" />
</rule>
In order to redirect the incoming request to another domain by using Rewrite action type (stay the old URL in the browser address bar), we need to install Application Request Routing module.
https://www.iis.net/downloads/microsoft/application-request-routing
By default, Rewrite action only forwards these requests to the same domain, therefore, we only can specify a URL path in the Rewrite URL field.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#rewrite-action
Otherwise, Redirecting the incoming request to another domain will cause a 404 error.
After we installed the ARR extension, enable Reverse Proxy functionality following the below steps.
Here is an official example.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing
Finally, please refer to the below configuration.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Rewrite" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
It will redirect the requests that has “/test” segment to the https://www.bing.com/maps.
If we want to complete the task without installing ARR extension, we have to use Redirect action type. The URL in the browser address bar will change while we access the qualified URL.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Redirect" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there is anything I can help with.

301 redirects after domain name change

I run a small ASP.NET4 website on IIS. I recently changed the domain name from 'olddomain.com' to 'newdomain.com'. The change was made through Plesk. After making the change I added 'olddomain.com' as a domain alias in plesk so that traffic would still route through to the website on the old domain.
This is fine so far. The website resolves on both old and new URLs.
However, now I would like to set up 301 redirects everywhere so that any request for olddomain.com are forwarded to newdomain.com instead. None of the website pages have changed - it's just a simple change of domain name.
I've added this to the web.config file:
<rewrite>
<rules>
<rule name="Redirect all to different domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^olddomain.com$" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it doesn't seem to have any impact; when I load olddomain.com in the browser it just loads the website without redirecting to newdomain.com.
Can anyone suggest what I've done wrong here and how to get the 301 to work?
Thanks
This works:
<rule name="redirectDomain" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?olddomain\.com$" />
</conditions>
</rule>
You can set 301 permanent redirect from IIS also.
If http redirection is enabled on old server then you have to put new web config with the contents -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Let me know if it helps.

IIS7 Rewrite Rule to redirect http://example.com to https://www.example.com

I want the following to redirect to https://www.domain.com
http://example.com
http://www.example.com
I used the default rules in IIS for the canonical domain redirect, which uses this pattern :
^www\.domain\.com$
This works fine for the first one. The www domain does not redirect. I tried removing the ^ but that doesnt work. How do I fix this?
Suppose you have URL-Rewrite module on IIS7?
Typical example for HTTP to HTTPS redirection:
<rule name="Redirect 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>
Can you try?

Resources