【发布时间】:2017-09-28 10:56:22
【问题描述】:
我正在尝试针对以下 3 个条件重写我的 URL: 1.删除.php扩展名 2.删除.html扩展名 3.将所有“www”重定向到非www
我尝试使用 ASP Core 的 Rewrite 包并运行以下代码:
app.UseRewriter(new RewriteOptions().AddIISUrlRewrite(env.WebRootFileProvider, "files/IISUrlRewrite.xml"));
在文件/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>
但是,这不起作用。 www 没有被重定向,.php 没有被删除。当我把它放在我的 web.config 文件中时,这段代码可以正常工作。
另外,我知道我可以使用中间件 Microsoft Documentation: Url Rewriting 做同样的事情
感谢您的帮助。
【问题讨论】:
-
您的第二条规则,即 .html 页面重定向是否有效?
-
感谢您的回复,不,这也不行。
-
您的项目结构中的哪个位置恰好是
files/IISUrlRewrite?如果它在您的项目根目录中,您需要使用env.ContentRootFileProvider,使用env.WebRootFileProvider意味着它将位于$/wwwroot/files/IISUrlRewrite。您还可以通过手动检查fileProvider.GetFileInfo(filePath);来仔细检查是否使用这些文件提供程序中的任何一个找到该文件 -
是的,它在 /wwwroot/files/IISUrlRewrite 中。在启动时,它没有抛出文件未找到异常,因此我很确定它能够正确定位火灾。
标签: c# url-rewriting asp.net-core asp.net-core-mvc