2

I am trying to change my old domain to new one and I have huge data on my website. I need to change only my domain by url rewriting.

When I request:

www.myolddomain.net/article/seo-friendly-url-for-this-article

I need to have a permanent (301) redirect to:

www.mynewdomain.com/article/seo-friendly-url-for-this-article

How ca I do this in asp.net core?

4
  • What have you tried so far? Commented Apr 24, 2019 at 20:11
  • Without knowing your situation I would suggest responding with statuscode 301 and the Location header see MDN Commented Apr 24, 2019 at 20:41
  • Use statuscode 307 for a temporary redirect and 301 for a permanent (the browser will remember) Commented Apr 24, 2019 at 20:42
  • @hatef - have you tried the approach below? Let me know how it goes. Commented Apr 26, 2019 at 18:54

1 Answer 1

6

Have you considered URL Rewriting Middleware?

It's pretty simple.

  1. Drop a IISUrlRewrite.xml file in the root of your application folder. Mark it as "Content" and "Copy to output directory" set to true, looks like this in your csproj
  <ItemGroup>
    <Content Include="IISUrlRewrite.xml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  1. Add following content in the file
<rewrite>
  <rules>    
    <rule name="Host replace - Old to new" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="www\.myolddomain\.net" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://www.mynewdomain.com{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
    </rule>
   </rules>
</rewrite>
  1. Register the URL rewrite module in the Configure method of your Startup.cs file
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{   
    // Irrelevant code omitted

    using (var iisUrlRewriteStreamReader = File.OpenText(Path.Combine(env.ContentRootPath, "IISUrlRewrite.xml")))
    {
        var options = new RewriteOptions().AddIISUrlRewrite(iisUrlRewriteStreamReader);
        app.UseRewriter(options);
    }

    // Irrelevant code omitted
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.