简体   繁体   English

ASP核心:使用AddIISUrlRewrite时URL重写不起作用

[英]ASP Core: URL Rewrite not working when using AddIISUrlRewrite

I am trying to re-write my URL for the 3 following condition: 1. Remove .php extention 2. Remove .html extention 3. redirect all "www" to non-www 我正在尝试针对以下3种情况重新编写我的URL:1.删除.php扩展名2.删除.html扩展名3.将所有“ www”重定向到非www

I was trying to use the ASP Core's Rewrite package and run the following code: 我试图使用ASP Core的Rewrite包并运行以下代码:

app.UseRewriter(new RewriteOptions().AddIISUrlRewrite(env.WebRootFileProvider, "files/IISUrlRewrite.xml"));

on files/IISUrlRewrite.xml, I have this code: 在文件/IISUrlRewrite.xml上,我有以下代码:

<rewrite>
  <rules>
    <rule name="RemovePHP">
      <match url="^(.*).php$" />
      <action type="Rewrite" url="{R:1}" />
    </rule>
    <rule name="RemoveHTML">
      <match url="^(.*).html" />
      <action type="Rewrite" url="{R:1}" />
    </rule>
    <rule name="www to non-www" stopProcessing="true">
      <match url="(www.*)" negate="false"></match>
      <action type="Redirect" url="http://example.com/{R:1}"></action>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true"></add>
      </conditions>
    </rule>
  </rules>
</rewrite>

However, this is not working. 但是,这不起作用。 www's are not being redirected, .php are not being removed. www不会被重定向,.php不会被删除。 This code works fine when I put it in my web.config file. 当我将其放在web.config文件中时,此代码可以正常工作。

Also, I understand I could do the same thing using the middleware Microsoft Documentation: Url Rewriting 另外,我知道我可以使用中间件Microsoft文档做同样的事情:URL重写

Thank you for the help. 感谢您的帮助。

This usually happens when you add the RewriteMiddleware after configuring the static file handling using the app.UseStaticFiles() method: if you do that, the RewriteMiddleware might be superseeded by the one in charge of the static files, which could mess up your rewrite rules. 在使用app.UseStaticFiles()方法配置静态文件处理后添加RewriteMiddleware时,通常会发生这种情况:如果这样做,则RewriteMiddleware可能会由负责静态文件的人取代,这可能会破坏您的重写规则。

In other words, do not do this: 换句话说, 要这样做:

app.UseDefaultFiles();

app.UseStaticFiles(new StaticFileOptions(...));

app.UseRewriter(new RewriteOptions(...)); // <- MIGHT NOT WORK AS EXPECTED

And do this instead: 而是这样做:

app.UseDefaultFiles();

app.UseRewriter(new RewriteOptions(...));  // <- WORKS

app.UseStaticFiles(new StaticFileOptions(...));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor ASP.Net Core(服务器端)中的自定义 URL 重写规则在使用导航链接时不会触发 - Custom URL rewrite rule in Blazor ASP.Net Core (server-side) not triggering when using navlink 主机头的 URL 重写在 asp.net 中使用 IIS 不起作用 - URL Rewrite for Host Header is not working using IIS in asp.net 在asp.net核心中使用返回URL - Working with return url in asp.net core asp.net核心2.1跨组织策略。 添加为CORS时,仅单个网址有效 - asp.net core 2.1 cross orgin policy. Only single url working when added as CORS 如何使用ASP.NET 4.0 URL重写来重写URL? - How to rewrite URL using of ASP.NET 4.0 URL rewriting? ASP.Net MVC:如何在 ASP.NET Core 中通过中间件重写 url - ASP.Net MVC:How to rewrite url by middleware in ASP.NET Core Request.Url.AbsoluteUri在ASP.NET Core 2.0中不起作用(使用System.Web) - Request.Url.AbsoluteUri not working in asp.net core 2.0 (Using System.Web) 如何在.net core中重写URL? - How to rewrite URL in .net core? 使用具有多个参数的asp.net路由器重写url - url rewrite using asp.net routers with multiple parameter 我可以在 ASP.NET Core 项目上使用带有 web.config 的 URL 重写吗? - Can I use URL Rewrite with web.config on ASP.NET Core Projects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM