Redirect website from http to https - c#

I have a website stored on azure for which I got an SSL certificate. I am trying to redirect www.mywebsite.com to https://www.mywebsite.com but with no success.
I am using the following code that should change the configuration of IIS where my project is deployed :
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
But when I type my URL it does not redirect to https.
By the way, rewrite appears unrecorgnized by Intellisense.

I can see that you've taken the code snipet from Steve Marx example
The problem is that you have whitespace in your rule name. Just remove them and it will work.
The name attribute of rule must not have spaces; the rule won’t work correctly in IIS on Azure with spaces in the name.
Find the full article here:
Azure https guide

Related

Alternate way to "<rewrite>" web.config. How to accomplish it programatically. WCF

I have the following two rules for my WCF:
<system.webServer>
<rewrite>
<rules>
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
The first rule:
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
Rewrites calls that get sent without the svc extension to the svc service.
The second rule:
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
Removes an extra path and directs to the correct service.
Everything works fine, however, the site I will be publishing to does not allow the <rule> tag to be used in the web.config. My question is, how can I modify my WCF to accomplish the two rules above programmatically. Basically duplicating the logic of the two items mentioned above in C# as part of the WCF service. Thank You.
I found the solution!
Very easy to fix, first remove the entire <rewrite> section from your web.config. Then just add new file (type global.asax) to your project if you do not already have it.
Add the following code in the Application_BeginRequest method:
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Raw URL
HttpContext context = HttpContext.Current;
string rawUrl = context.Request.RawUrl;
// Extra Path to be removed
const string EXTRA_PATH = "/MoreServices";
// Rewrite requests to /MyService to /MyService.svc
if (!rawUrl.Contains(".svc"))
{
// Index of last forward slash
int idxLastSlash = rawUrl.LastIndexOf('/');
// Insert .svc before last slash
rawUrl = rawUrl.Insert(idxLastSlash, ".svc");
}
// Remove extra path EXTRA_PATH from url
if (rawUrl.Contains(EXTRA_PATH))
{
// Remove extra path
rawUrl = rawUrl.Replace(EXTRA_PATH, "");
}
// Rewrite Path if it has been modified
if (rawUrl != context.Request.RawUrl)
{
// Note: Added "~" to avoid error: "The virtual path XX maps to another application, which is not allowed."
context.RewritePath("~" + rawUrl, false);
}
}
This will work the same way as the two rules above in the web.config. Hope this will help someone.

non-www with trailing slash 301 redirect in web.config (Single Redirect)

How can I 301 redirect my URLs to non-www equivalent with trailing slash at the end with only a single 301 redirect (avoid redirect chain)? I am using ASP.net 4.5 / C# / Web Project with my routes registered in the RouteConfig.cs.
One option is to check the URL in code behind for each page and rebuild the URL, but I prefer letting IIS handle it using Rewrite rules.
As you can see from this image (via chrome, client side), there are two 301 redirects, because in my web.config I have two rules, one for changing to lowercase URL and second to add trailing slash.
Maybe there is an option in IIS to prevent redirection until all the URL rewrites ran internally. I searched for it, but couldn't find a solution yet.
In web.config (you need to have IIS URL Rewrite module installed)
<system.webServer>
<rewrite>
<rules>
<rule name="noslash" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".+[^\/]$" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="www.yourdomain.com" />
<add input="{HTTP_HOST}" pattern="yourdomain.com" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:0}/" />
</rule>
<rule name="www" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".+\/$" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="www.yourdomain.com" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT2: added trailing slash at the end of the url if it doesn't exist

ReRouting Http to Https in MVC 3.0 Using Rules Not Working

I have to route all the requests on my site from http to https, I was succesfull in doing that by adding another class in Global.Asax.Cs
// working part for redirecting http into https
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
But before that I tried by inserting Rules into WebConfig
<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>
Which is not working, But I have seen many posts here which say that Rules is the best way to route request, I have also installed URL Rewrite on my server . Cany any one please suggest why it wasnt working ( i am getting 500 internal server error).
Does rerouting in Global.asax affect performance when there is a huge volume of requests.
Please advice
I use this rule and it works like a charm:
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
without any datailed info about the error i can only guess that your web.config is malformed or you put the rule section in the wrong place or URL Rewrite Module is not installed correctly

URL Rewrite to remove www and redirect to https using web-config (c# .net)

I have the following code in my web-config to be able to redirect both the URLs with the prefix "www" and non-SSL requests to the https:// mydomain.com because the SSL certificate is registered to the domain without the www
<rewrite>
<rules>
<rule name="Remove WWW prefix and redirect to https" >
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" ignoreCase="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
This is the result:
1) http:// mydomain.com/something --> https:// mydomain.com/something (Correct)
2) http:// www.mydomain.com/something --> https:// mydomain.com/something (Correct)
3) https:// www.mydomain.com/something --> Shows certificate error (There is a problem with this website's security certificate.)
When you select "Continue to this website (not recommended)." on the certificate error page, the url is rewritten correctly (https:// mydomain.com/something)
How can I make sure the certificate error does not show?
Thank you
One way to solve it is to register two separate rules:
Remove www.
Force HTTPS.
<rule name="Remove www" stopProcessing="true">
<match url="(.*)" negate="false"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
so we use this in our projects, and this works.
Let me know it that helps:
<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}" pattern="localhost" negate="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
It also ignores the request when you access the site on your local machine.
I'm not sure about this as I don't have too much experience with url-rewriting but trying to help wont hurt.
You can try this
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://mydomain.com/{R:1}" redirectType="Permanent" />
I googled quite a lot and found this but it might not do what you meant to.
This problem cannot be solved by using rewrite rules: the problem is that the certificate is verified at the time the connection to the server is set up. As your server does not have a valid certificate for the www. variant, the certificate is invalid and the browser will notify its user.
Only after the user agrees to continue, the request is sent to the server and the rewrite rules kick in.
I'm seeing the same problem. Because the domain doesn't have an SSL certificate for www, the web.config code doesn't remove the www when the URL includes https. The result is using http with or without the www, correctly redirects it to https://domain, but if it starts with https: and www, it's stuck.
So can this be resolved at the DNS level so that www isn't defined as a CNAME and is just redirected there? Google Domains seems to have synthetic records for this. Has anyone used it successfully?

URL Rewrite on IIS always fail when change the pattern

I'm trying to work in a rewrite problem, but its my first time and I guess I'm doing something very wrong.
A few users of our portal, try to navigate typing https://www.dudalina.proveagora.com instead the right one https://dudalina.proveagora.com. We have the SSL only for *.proveagora.com, so I started to try a redirect on the users who type the wrong url.
Here, the print of rewrite working great:
but, some users type https:// and some users type http://. By this way, I've tried to change the pattern.
I dont know why, but using the second pattern, the url https://www.dudalina.proveagora.com doesnt redirect anymore.
Just to explain a little more, the dudalina part is dynamic too. Dudalina is the store name, and it will change accordingly with store. So, we can have latter something like http://bestbuy.proveagora.com or anything else and this patterns much works with all the options.
I know, the user cant type www.bestbuy.proveagora.com but they do, and we need to make it works! :(
Any help?
EDIT
xml Web.Config
<rewrite>
<rules>
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(https\:\/\/|https\:\/\/)?([a-zA-Z]+\.)?(proveagora\.com){1}(\/.*)?$" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="http://dudalina.proveagora.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Here you go:
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.+\.)proveagora\.com$" />
<add input="{HTTP_HOST}" pattern="^dudalina\.proveagora\.com$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="https://dudalina.proveagora.com" appendQueryString="false" />
</rule>
What it will check is that the protocol used is HTTPS and that the subdomain of proveagora.com is present and different from dudalina.
If that's the case, it redirects to https://dudalina.proveagora.com

Categories