ASP.NET Core url 仅重写域 [英] ASP.NET Core url rewrite only domain

查看:24
本文介绍了ASP.NET Core url 仅重写域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将旧域更改为新域,并且我的网站上有大量数据.我只需要通过 url 重写来更改我的域.

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.

当我请求时:

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

我需要永久 (301) 重定向到:

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

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

如何在 asp.net core 中执行此操作?

How ca I do this in asp.net core?

推荐答案

您是否考虑过 URL 重写中间件?

很简单.

  1. 在应用程序文件夹的根目录中放置一个 IISUrlRewrite.xml 文件.将其标记为内容"和复制到输出目录"设置为 true,在你的 csproj 中看起来像这样

  <ItemGroup>
    <Content Include="IISUrlRewrite.xml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  1. 在文件中添加以下内容

<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. Startup.cs 文件的 Configure 方法中注册 URL 重写模块
  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
}

这篇关于ASP.NET Core url 仅重写域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆