Redirection from "https://www" to "https://" - asp.net

We have the ASP.NET application hosted as Azure WebApp and configured the domain from GoDaddy. We bought SSL for non-www domain. We used the following redirection rule:
<rewrite>
<rules>
<rule name="Redirect to Example.com">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!^Example.com)" />
</conditions>
<action type="Redirect" url="https://Example.com/{R:1}"/>
</rule>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
Is there a way to add another rule to redirect request to "https://www" to "https://"?
Thanks,
Jay

I had a similar challenge with my Windows hosting since I have a wildcard cert and host a few subdomains. Here's the web.config I put together, give it a shot and let me know! It seems to work great for me, but my site is extremely simple.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<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>
<rule name="remove www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.YourDomain\.com$" />
</conditions>
<action type="Redirect" url="https://YourDomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

web config rewrite rule to force http to https on multi tenancy application

i have a multi tenancy portal system but the rewrite rule is not working on multiple conditions
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTPS1}" pattern="^(website1\.com|www\.website1\.com)$" />
<add input="{HTTPS2}" pattern="^(website2\.com|www\.website2\.com)$" />
<add input="{HTTPS3}" pattern="^(website3\.com|www\.website3\.com)$" />
<add input="{HTTPS4}" pattern="^(website4\.com|www\.website4\.com)$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
I expect That when i visit http://portal.website1.com on my webs browser, that the program should take me to https://portal.website1.com and to do the same for portal.website2.com, portal.website3.com,portal.website4.com
but it only works for portal.website2.com and portal.website3.com , it does not force https on portal.website1.com and portal.website4.com
Try this in system.WebServer:
<system.webServer>
....
<rewrite>
<rules>
<rule name="Force HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URO}" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>

URL rewrite web.config with location https

I want to redirect my extranet site to https I have the following web.config rule:
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" `enter code here`ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
The problem comes because I have several services hosted on my server that are children, and I don't want them to redirect too, I just want to redirect the site not the services.
One way to do this is to by default redirect to HTTPS except for a specified list of folders. In the below example, folders /service1/ and /service2/ are excluded from redirection. Everything else goes to HTTPS.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<!-- avoid redirection for the following paths -->
<add input="{PATH_INFO}" pattern="^/service1/" ignoreCase="true" negate="true" />
<add input="{PATH_INFO}" pattern="^/service2/" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>

How to re-write subdirectory to subdomain

I tried but fail to do:
<rewrite>
<rules>
<rule name="blog url rewrite">
<match url="(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomin.com/blog" />
<add input="{REQUEST_URI}" pattern="^mydomin/blog" />
</conditions>
<action type="Rewrite" url="cdn.mydomin.com/{C:1}" />
</rule>
</rules>
</rewrite>
Can anyone please help me? I have asp.net webform application 4.5
And I want to re-write: mydomain.com/blog to blog.mydomain.com
Try this:
<rules>
<rule name="blog url rewrite" stopProcessing="true">
<match url="blog" />
<action type="Redirect" url="/blog.{C:1}" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/(.*)/blog.*" />
</conditions>
</rule>

How do I redirect all but a single url to https in ASP.Net?

I have the following code in my web.config:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
I would like to redirect everything EXCEPT for http://www.mysite.com/manual to https.
How would I modify the above to allow this?
It should be ok with adding the following code in your conditions tag
<add input="{REQUEST_URI}" negate="true" pattern="^/manual/*" ignoreCase="true" />

Removing index.cfm from url with web config

quick question -
Currently my urls look like this: index.cfm/camp/another-test
I would like for them to look like this: camp/another-test
I'm able to do this fine on apache with my .htaccess but I need to be able to do it on iis7 with the web.config. Here's my rewrite so far:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
Thanks for the help!
I believe CFWheels requires that you route rewrite requests through rewrite.cfm not index.cfm.
See the comment by Chris Peters on this question
If you adjust:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
to:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" matchType="Pattern" ignoreCase="true" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
it should solve your problem, provided you have:
<cfset set(URLRewriting = "On")>
within /config/settings.cfm
Try adding this rewriting rule:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|newsletters|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>

Resources