Forcefully open site to www only . - asp.net

I had hosted site in IIS which open in url like http://www.example.com as well as http://example.com
but i dont want to open it in http://example.com ,
http://example.com might change to http://www.example.com Forcefully, what confugeration shoud i wright in web.config

(Note: Updated answer below)
This requires you to make use of URL-rewriting.
This post here shows how to make use of the URL-Rewriting extensions:
http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html
If you don't see this extension via IIS manager, you can download the extension here:
http://www.iis.net/download/URLRewrite
You will want to add a rule, look under "search engine optimization" and edit "canonical domain name".
There you can direct all traffic to the www-version of the domain by selecting it in the dropdown.
EDIT:
As for the question to do it in web.config, this is an example configuration:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Hope that helps,
Sebastian

Related

Azure redirect non-www to www

I have question about Azure redirection.
I have searched other topics but not quite my problem(s).
I have ASP.NET Core 2.2 project and Azure configured for that. Also i have domain for pointing to Azure.
I have added www.example.com format domain to azure and that works fine also with https.
Problem rises with example.com format
I wanted to redirect example.com format to www.example.com URL
I manage to get this work with this, but for little while.
I use this web.config for redirection
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Something strange is Google Chrome & Opera redirect(s) to www domain but not Edge and Firefox.
Then when i clear cache(s) then it wont redirect.
Seems like Azure like rewrites some over or something.
Thanks for help!!
In addition to the web.config file changes, ensure the Azure Website instance contains the correct domain bindings within the Hostname bindings area.
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>

Http to Https for certain URLs using IIS

ASP .Net website. Visual Studio 2013. Windows Server 2012 R2 + updates
I am attempting to redirect SOME pages as https i.e. login and payment pages AND pages under those directories. So far in IIS i have
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^.example\.com$" />
<add input="{HttpsRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{C:1}" appendQueryString="false" redirectType="SeeOther" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HttpsRedirects">
<add key="/user/login/" value="/user/login/" />
<add key="/user/payment/" value="/user/payment/" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
I have changed pattern="^www.example.com$" to pattern="^.example.com$" as the site's main domain name is example.com (without www).
The problem i have is it doesnt seem to add https to all pages below these directories i.e. all pages under the login and payment directories?
IF I manually add https then it also applies it to image files (im seeing images not looking correct).
How could i amend the above code to exclude images (file extension or directories) and ensure when the user reaches these pages they are using https?

IIS redirect http to https for domain only

I have created and ASP.NET Core project and i purchased a SSL for my website. I have created a redirect rules in my web.config to redirect my http to https. The issue i have is that every link on my website now have https which cause certificate issues since some of those links don't have ssl. This is what i've done:
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="Off" />
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
<add input="{HTTP_HOST}" negate="true" pattern="localhost" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
For example, my website domain is www.example.com. After enabling SSL on my project, my website is now https://www.example.com. I have some links on my website for my partners such as www.partner1.com. The problem is that now my partners link also have https in their url but their website does not have SSL certificate which means when i click on their links, i am redirected to a https url and therefore i have a certificate error page.
How can i set my domain to only have https and let my partners a href link still use http?
i would suggest you to use this same rule:
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true"> <match url=".*" />
<conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rewrite>
because i am using same and i already check with your scenario it's working perfect..as you mentioned in your answer.
i created a link on my website page and set that a href to a http:// domain which is another website. its not changing and on click that link working fine. here is link code:
<a target="_blank" href="http:// www.jwoodmaker.com/">Test</a>
as i already send you link which have full details of this. this link

IIS redirect from subdomain to path

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html
Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

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.

Resources