---
title: Adding an administration redirect to ASP.NET Core sites
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

To give content editors and other staff easier access to the administration application from the frontend Core site, you can set up a redirection mechanism. The redirect can give users the illusion of working in a single application. For example, you can set it up so that the administration application is available under **\~/admin**, similar to other content management systems.

To configure the redirection mechanism, we recommend using the [URL rewriting](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting) middleware together with a custom rewrite rule:

1. Add the rewriting middleware to your application's middleware pipeline.
   - For optimal performance, add the middleware before the routing middleware (UseRouting). The rewrite rule performs a permanent ([HTTP 301](https://httpstatuses.com/301)) redirection, which renders the routing process pointless. Outside of this, the specific placement of the middleware largely depends on the configuration of your middleware pipeline. For more information, refer to [Microsoft's documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting#url-redirect-and-url-rewrite) on this topic.
2. Create a new URL rewriting rule. This example uses a rule called **AdminRedirect**.

   ```csharp title="Rewrite rule"

   using System;
   using System.Net;

   using Microsoft.AspNetCore.Rewrite;
   using Microsoft.Extensions.Configuration;
   using Microsoft.Net.Http.Headers;

   public class AdminRedirect : IRule
   {
       private readonly string adminUrl;

       public AdminRedirect(IConfiguration configuration)
       {
           // Retrieves the 'AdministrationUrl' application setting from the application's configuration providers (e.g., appsettings.json)
           adminUrl = configuration["AdministrationUrl"] ?? String.Empty;
       }

       public void ApplyRule(RewriteContext context)
       {
           if (String.IsNullOrEmpty(adminUrl))
           {
               return;
           }

           var request = context.HttpContext.Request;

           // Redirects requests leading to '~/admin' to the URL specified in the 'AdministrationUrl' setting
           // For example: https://administration.mydomain.com
           if (request.Path.Value.TrimEnd('/').Equals("/admin", StringComparison.OrdinalIgnoreCase))
           {
               var response = context.HttpContext.Response;

               response.StatusCode = (int)HttpStatusCode.MovedPermanently;
               response.Headers[HeaderNames.Location] = adminUrl;
               context.Result = RuleResult.EndResponse;
           }
       }
   }

   ```
3. Add the URL of the site's corresponding administration application under the _AdministrationUrl_ settings key. You can place the key into any [configuration provider](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration) used by your application (e.g., _appsettings.json_).

   ```xml title="Application configuration file (in the .json format)"

   {
       ...
       "AdministrationUrl": "administration.mydomain.com",
       ...
   }

   ```
4. Add the create rule to the collection of URL rewriting rules used by the middleware

   - The rewrite rule requires the **IConfiguration** provider to retrieve the URL of the administration interface from application settings. You may need to extend the signature of the **Configure** method with the _IConfiguration_ parameter. This ensures the _IConfiguration_ provider is available (provided via dependency injection) during application initialization.

     ```csharp title="Application startup class"

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
     {
         ...

         app.UseRewriter(new RewriteOptions()
                             .Add(new AdminRedirect(configuration)));

     `   ...

         app.UseRouting();

         ...
     }

     ```
5. Compile your project.

The redirection rule is now registered and ready. Requesting **\~/admin** redirects the request to the URL specified by the _AdministrationUrl_ setting. You can modify both the path under which the redirection occurs and the name of the setting key to suit your needs.
