2
我想重新寫我的網址爲以下3個條件時,不工作: 1.刪除.PHP extention 2.拆下的.html extention 3.重定向所有的「WWW」到非wwwASP核心:URL重寫使用AddIISUrlRewrite
我試圖用ASP核心的重寫包和運行下面的代碼:
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
謝謝你的幫助做同樣的事情。當你配置靜態文件中使用app.UseStaticFiles()
方法處理後添加RewriteMiddleware
是你的第二個規則,即html的網頁重定向的工作? – CPR43
感謝您的回覆,不,那也不行。 – FerX32
哪裏在你的項目結構是exacly'files/IISUrlRewrite'?如果這在你的項目根目錄下,你需要使用'env.ContentRootFileProvider',使用'env.WebRootFileProvider'就意味着它位於'$/wwwroot/files/IISUrlRewrite'中。你也可以通過手動檢查'fileProvider.GetFileInfo(filePath);' –