The UseHttps setting is exclusively for the backoffice to redirect to https, among a handful of other things it does. If you want to enforce Https redirecting for serving content too, you can make use of the HttpsRedirection middleware.
For more details check out:
https://our.umbraco.com/documentation/Reference/Security/SSL-HTTPS/
For redirecting non-www traffic you could make use of a URL rewrite rule. The Umbraco Docs has some excellent examples on rewrite rules, including non-www redirecting!
I haven't got access to log files so I can only see this from the "frontend":
HTTP Error 500.30 - ASP.NET Core app failed to start
Common solutions to this issue:
The app failed to start
The app started but then stopped
The app started but threw an exception during startup
Troubleshooting steps:
Check the system event log for error messages
Enable logging the application process' stdout messages
Attach a debugger to the application process and inspect
For more information visit: https://go.microsoft.com/fwlink/?LinkID=2028265
Where have you put your IISUrlRewrite.xml file? Is it in the root folder?
Does it work in dev/test but not prod? Could it be a file permissions issue?
I've used this which works for me:
var options = new RewriteOptions();
if (File.Exists("IISUrlRewrite.xml"))
{
using (StreamReader iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml"))
{
options.AddIISUrlRewrite(iisUrlRewriteStreamReader);
}
}
I checked if the "IISUrlRewrite.xml" was in the root on the server (outside wwwroot folder) and it was not. So webdeploy haven't deployed it.
So I tried to remove your File.Exists check: Then the same error occured.
But then putting back the File.Exists check site came up again.
Then I uploaded the file manually. AND tried to remove the File.Exists check. And the site still worked.
Then I tried to remove the IISUrlRewrite.xml and the site went down again.
So I think that the conclusion can be: Now the XML file can be reached by startup.cs, but Umbraco/the site ignores the rules inside the XML file (since http isn't redirected to https, and the same goes for www/non-www)
Sorry, I may have missed a line underneath, can you try this:
var options = new RewriteOptions();
if (File.Exists("IISUrlRewrite.xml"))
{
using (StreamReader iisUrlRewriteStreamReader = File.OpenText("IISUrlRewrite.xml"))
{
options.AddIISUrlRewrite(iisUrlRewriteStreamReader);
}
}
app.UseRewriter(options);
Umbraco 9 http to https redirect doesn't work
Hi,
In appsettings.json I have added:
but it doesn't work.
How to fix? And can I have www to non-www redirecting also in appsettings.json?
Hi Martin,
The UseHttps setting is exclusively for the backoffice to redirect to https, among a handful of other things it does. If you want to enforce Https redirecting for serving content too, you can make use of the HttpsRedirection middleware. For more details check out: https://our.umbraco.com/documentation/Reference/Security/SSL-HTTPS/
For redirecting non-www traffic you could make use of a URL rewrite rule. The Umbraco Docs has some excellent examples on rewrite rules, including non-www redirecting!
https://our.umbraco.com/documentation/Reference/Routing/IISRewriteRules/
Hope this helped,
Corné
Thanks - that looks right.
But I get an error on PROD when deploying this XML:
I haven't got access to log files so I can only see this from the "frontend":
In startup I have added:
just before
And this in the top
Where have you put your IISUrlRewrite.xml file? Is it in the root folder?
Does it work in dev/test but not prod? Could it be a file permissions issue?
I've used this which works for me:
Thanks - I am on the track now, I think:
I checked if the "IISUrlRewrite.xml" was in the root on the server (outside wwwroot folder) and it was not. So webdeploy haven't deployed it.
So I tried to remove your File.Exists check: Then the same error occured. But then putting back the File.Exists check site came up again.
Then I uploaded the file manually. AND tried to remove the File.Exists check. And the site still worked.
Then I tried to remove the IISUrlRewrite.xml and the site went down again.
So I think that the conclusion can be: Now the XML file can be reached by startup.cs, but Umbraco/the site ignores the rules inside the XML file (since http isn't redirected to https, and the same goes for www/non-www)
Could your rule be wrong?
Your rule is negating the www domain, so it won't run the rule.
My HTTPS redirect rule is:
Which works for me.
It doesn't work, and I have just updated the XML-file:
My Startup.cs is:
That's odd. Does it fail is you deliberately put a mistake in the XML file? Just to prove the file is actually being found and loaded.
I tried that now (adding some text outside tags):
It changed nothing (the site still runs fine). Then I tried once again to remove the File.Exists check and then:
So it seems that the file is fetched by StreamReader, but it isn't read (or isn't read as an XML file).
Have anyone tried that or have suggestions?
Sorry, I may have missed a line underneath, can you try this:
Cool! Thanks, that made the rewrites being processed.
The www redirect sort of works, but it adds www to already www urls. I.e.
www.mysite.com becomes www.www.mysite.com
And if I chance it to the content from https://our.umbraco.com/documentation/Reference/Routing/IISRewriteRules/:
I get the same error as in the beginning:
Hi Martin,
This works fine for me:
Are you able to put a try/catch around the code and log any errors?
Your rule did not negate www - hence why it was adding www twice.
Thanks for the tip: Can I log to Umbraco log fra Startup.cs, and how do I do it?
Hi Martin.
Does your webserver use a reverse proxy?
Hi Martin,
You should be able to inject a logger into your Configure method and write to the log:
You are also able to do this without an XML:
Thanks - that works for me! :)
is working on a reply...