URL Rewrites in Umbraco
With the release of Umbraco 9 and the change of the underlying web framework that is decoupled from the webserver, the way that you configure rewrites has changed as well.
Instead of the URL Rewriting extension in IIS you can use the URL Rewriting Middleware in ASP.NET Core, which needs to be added to your project startup code first.
If you are running Umbraco 9 on IIS you can still add a web.config
file to configure IIS features such as URL rewrites.
When to use the URL Rewriting Middleware
Make sure to check the official URL Rewriting Middleware in ASP.NET Core documentation for more information about when you should or should not use the URL Rewriting Middleware.
Using the URL Rewriting Middleware
To use rewrites with Umbraco 9 you have to register the middleware in your Startup.cs
by using the UseRewriter
extension method and then configure the rewrite options.
Example
Create an
IISUrlRewrite.xml
file in the root of your project (next to yourStartup.cs
file) containing:
In the
Startup.cs
file you can add the URL Rewriting Middleware just before the call toapp.UseUmbraco()
and useAddIISUrlRewrite
) to add the rewrite rules from the XML file:
On Linux, make sure to place the app.UseStaticFiles()
after the app.UseUmbraco()
statements for the redirect to work as intended.
In your csproj file add the XML file to a new item group and set
CopyToOutputDirectory
toAlways
:
On Umbraco Cloud the item group needs to be set to <CopyToPublishDirectory>Always</CopyToPublishDirectory>
for the file to be published to your deployed site.
Rewrite rule shortcuts
RewriteOptions
has a number of "shortcut" methods to implement commonly used rewrites including:
AddRedirectToNonWww()
AddRedirectToWww()
AddRedirectToNonWwwPermanent()
AddRedirectToWwwPermanent()
AddRedirectToHttps()
For more details and other examples, take a look at the URL Rewriting Middleware in ASP.NET Core and RewriteOptions Class documentation.
Examples of rewrite rules
External Resources
If you are looking for additional inspiration or examples for creating IIS Rewrite rules, these external resources are a great starting point:
A great site showing 10 handy IIS Rewrite rules: URL rewriting tips and tricks
Another site showing some handy examples of IIS Rewrite rules: Some useful IIS rewrite rules
If you needed to a lot of static rewrites using rewrite maps: Rule with rewrite map rule template
Example: Remove a Trailing Slash
The following rule removes any trailing slashes from the URL.
Ensure Umbraco does not add a trailing slash by setting AddTrailingSlash
to false
in your RequestHandler settings.
Example: Enforce HTTPS
The following rule ensures your site only runs on HTTPS:
Example: Redirect Non-www to www
The following rule redirects traffic from non-www to www (excluding the Umbraco Cloud project hostname):
Example: Remove the .aspx Extension
The following rule redirects .aspx
URLs to their extensionless counterparts.
Example: Custom Rewrite Rules for Umbraco Cloud
An example configuration to help ensure your custom rules integrate properly:
If you use Umbraco Cloud, check the Rewrite Rules article.
Last updated