How to stop double redirect on https and www in ASP.NET Core 6? - asp.net-core

I was using IIS rewrite rules in web.config to do my HTTPS and non-www canonical URI enforcement, but then using Web Deploy became complicated so I switched to ASP.NET Core 6 middleware. Here's my code from Program.cs:
app.UseRewriter(new RewriteOptions().AddRedirectToNonWwwPermanent());
app.UseHttpsRedirection();
For http://example.com and https://www.example.com it works great and there is a single redirect.
But, for http://www.example.com it now implements the non-www rule, then the https rule, so you get two 308 redirects chained together (with IIS I had this with a single redirect).
You can test it here: https://www.redirect-checker.org/index.php
Enter: http://www.magazedia.com/
http://www.magazedia.com
308 Permanent Redirect
http://magazedia.com/
308 Permanent Redirect
https://magazedia.com/
200 OK
This plays havoc with my OCD.
Is there a simple fix or do I need to write a custom redirect that combines both?
EDIT:
I also just tried this, but the result is the same (double redirect):
app.UseRewriter(new RewriteOptions().AddRedirectToNonWwwPermanent().AddRedirectToHttpsPermanent() );
// app.UseHttpsRedirection();

As far as I know, the asp.net core url rewrite middleware also support using the url rewrite rule xml.
You could create a url reiwte xml named IISUrlRewrite.xml and like below:
<rewrite>
<rules>
<rule name="Rewrite segment to id querystring" stopProcessing="true">
<match url="^iis-rules-rewrite/(.*)$" />
<action type="Rewrite" url="rewritten?id={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
Then modify the startup.cs(.net 5) or program.cs(.net 6)
using (StreamReader iisUrlRewriteStreamReader =
File.OpenText("IISUrlRewrite.xml"))
{
var options = new RewriteOptions()
.AddIISUrlRewrite(iisUrlRewriteStreamReader)
app.UseRewriter(options);
}
More details, you could refer to this article.

Related

ASP.Net Core 2.2 wwwroot static content hosted on S3 and CloudFront

I am using ASP.Net Core 2.2 and would like to host wwwroot static content on AWS S3 served by CloudFront. Haven't found anything on Google so posted the question here. I am familiar with AWS S3 and Cloudfront. I only need to find out how / what to change in ASP.Net Core razor application. Any advice and insight is appreciated.
I solved it by using rewrite rules in my server (IIS).
In order to do that you add a new IIS Url Rewrite module rule under system.webServer section in web.config.
Also, if you host your app in Apache/Nginx, there is a possibility to configure redirect rule based on configuration file with server-specific syntax.
Following is a sample rule, it redirects all resources starting with "media" to CloudFront distribution.
<rewrite>
<rules>
<rule name="Media to CDN" patternSyntax="Wildcard" stopProcessing="true">
<match url="media/*" />
<action type="Redirect" url="https://<my_cloudfront_id>.cloudfront.net/media/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

IIS URL Rewrite to redirect all requests for a domain

I have a web server that hosts multiple domains running IIS 8.5. One of those domains is going away and needs to be redirected to the root of another domain on the server. I created the rule below and it works fine as long as there is not path. If there is a path, it appends the path and results in a 404 error. I want to ignore the path and only redirect to www.DomainB.com. I would think path would only be included if I put in http://www.DomainB.com/{R:1} as the redirect url but this doesn't seem to be the case. Where am I going wrong here?
<rule name="Redirect DomainA.com">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*DomainA.com$" />
</conditions>
<action type="Redirect" url="http://www.DomainB.com/" redirectType="Found"/>
</rule>
I figured it out. The above is actually good. It was another rule for DomainB, one set to do a redirect from www to non www, which was causing the issue. It did not have DomainB in the condition, but rather a reg expression that matched any www and that rule does have the {R:1} on it, so the path was actually being appended by that rule after the DomainA.com rule processed. After making that rule DomainB specific, the above Redirect DomainA.com rule worked as is

Redirect old urls when changing to prefixless in Piranha Cms

I've unfortunately got a lot of pages SEO-indexed with "/home" as prefix.
Now I need to change to prefixless urls in Piranha, which is easy. But is there a place where I can force redirects from old urls to new urls?
E.g. redirect "www.example.com/home/page" to "www.example.com/page". The problem is that the old page doesn't exist anymore so I can't put a script on that page to do the redirect.
Best regards
Lars, Denmark
I think the best way is to use url rewrite module (need to install it from web platform).
In web.config You can append rewrite rules to system.webServer => rewrite => rules.
I will not test it, but it should be something like this:
<system.webserver>
<rewrite>
<rules>
<rule name="RedirectToPrefixless" stopProcessing="true">
<match url="^home/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
Tag "match" finds for urls begining with "home/" - part of regex "(.*)" create group which will be used in redirect url
Tag "action" states, that for matching URL it should be redirected to "(.*)" part from "match" part.
So for example:
/home/test1 redirects to /test1
/home/test2?page=22 redirects to /test2?page=23
All redirects are "Permanent" (http status 301) - it can be also changed ;)

URL Rewrite on Windows 2016 (IIS 10) with .Net Core to point to CDN

I have a brand new virtual server running Windows 2016 (IIS 10). I have a website that is built with .NET Core 1.1.
I would like to rewrite all image, js, font, and css references to point to a CDN.
Problem 1, adding this rule:
<rule name="CDN Assets" stopProcessing="true">
<match url="^css/(.+)$|^images/(.+)$|^fonts/(.+)$|^js/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="https://cdn.example.net/{R:0}" appendQueryString="true" />
</rule>
... results in a 404 for every request. For example a request to https://example.net/images/en-us/logo.png even though the image exists both in the real folder under and at https://cdn.example.net/images/en-us/logo.png
Is this possibly caused by .NET Core or by HTTP2 (which is introduced in IIS 10). Or do I need some sort of outbound rule also?
Problem 2, adding an outbound rule like:
<rule name="Outbound CDN: images" preCondition="IsHtml">
<match filterByTags="A, Img, Link" pattern="^/images/(.+)$" />
<action type="Rewrite" value="//cdn.example.net{R:0}" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
</preCondition>
</preConditions>
Causes the page response to become complete mangled unreadable garbage. Is this because of the response is sent over HTTPS?
UPDATE (2016/03/16 8:01PM MST)
Using #EricB suggestion I have tried implementing it via the Microsoft.ASPNetCore.Rewrite package to no avail.
I have this in my Startup.cs and just the local images are being loaded:
string CDNroot = Configuration["URLs:CDN"].Trim();
RewriteOptions rewriteOptions = new RewriteOptions()
.AddRewrite(#"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2", skipRemainingRules: true);
app.UseRewriter(rewriteOptions);
If I change this to:
string CDNroot = Configuration["URLs:CDN"].Trim();
RewriteOptions rewriteOptions = new RewriteOptions()
.AddRedirect(#"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2");
app.UseRewriter(rewriteOptions);
... and request an image URL it is correctly redirected to the CDN URL. But you wouldn't want a redirect, as that is adding an extra 30x response to every asset request. I am stumped why Redirect works, but Rewrite doesn't.

https://www.mydomain.com and https://mydomain.com go to different pages

Web Server: IIS 6.0 (ASP.Net 4.0)
I host two sites: one is the main web site and the other is the store site. Each are separate web sites in IIS. Each share the same wildcard SSL certificate. The store site uses a Host Header (store.mydomain.com) to direct traffic to it.
I want it so any URL used without the sub-domain "store" directs the user to the main web site, not the store web site.
The problem I'm experiencing is that the following URL always directs users to the default.aspx page on the store web site:
https://www.mydomain.com
Yet, these URLs correctly go to the main page on the main web site:
http://mydomain.com
http://www.mydomain.com
https://mydomain.com
What's up with the https://www that directs users to different page?
I have added a rewrite rule in the web.config file for both sites but it doesn't have any effect:
<rewrite>
<rules>
<rule name="Consistent Domain" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
First, are you sure this is IIS6? The URLRewrite feature is part of IIS7 and would have no effect under IIS6 if present. If this is IIS6 that may well be the answer to your question.
That aside, it is difficult to answer this based on the data given. It is possible that the bindings of the sites is incorrect and resulting in the traffic going to a site you don't expect.