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

查看:199
本文介绍了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核心中做到这一点?

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

  1. Register the URL rewrite module in the Configure method of your Startup.cs file



public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {   

// Irrelevant code emitted

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


// Irrelevant code emitted

        }

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

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