如何在多语言 ASP.NET 网站中重写 URL
How to rewrite URL in multi-lingual ASP.NET website
我想在我的 asp.net 多语言网站中使用 Web.config 和规则部分重写 URL。
我有 url 喜欢:
http://example.com/lang/en/index.html
、http://example.com/lang/fr/index.html
、等等
我需要删除 lang
和 .html
扩展名并将 url 重写为:
http://example.com/en/index
、http://example.com/fr/index
我的Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteHTML">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.html" />
</rule>
<rule name="Rewrite friendly URLs to phsyical paths">
<match url="^(.*)$" />
<action type="Rewrite" url="lang/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
所以如果我去'http://example.com/en/index' I need to open this page: 'http://example.com/lang/en/index.html'。
如何实现这个目标?
<rule name="the name" enabled="true" stopProcessing="true">
<match url="example.com/(.+)(?:/(.+)?)(.html)?"/>
<action type="Rewrite" url="lang/{R:1}/{R:2}.html"/>
</rule>
[更新]
终于解决了。这里是 Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="On" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Ignore" enabled="true" stopProcessing="true">
<match url="^(js|css).*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Redirect requests to friendly URLs">
<match url="^(.*?)/(.*)\.html$" />
<action type="Redirect" url="{R:2}" />
</rule>
<rule name="Rewrite friendly URLs to phsyical paths">
<match url="^\/(?!#.*).*" />
<action type="Rewrite" url="lang/{R:0}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
特别感谢 Ashkan Mobayen Khiabani 的 "Ignore" 部分(需要忽略:javaScript、图像和 CSS 文件夹以保持链接正常工作)
我想在我的 asp.net 多语言网站中使用 Web.config 和规则部分重写 URL。
我有 url 喜欢:
http://example.com/lang/en/index.html
、http://example.com/lang/fr/index.html
、等等
我需要删除 lang
和 .html
扩展名并将 url 重写为:
http://example.com/en/index
、http://example.com/fr/index
我的Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteHTML">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.html" />
</rule>
<rule name="Rewrite friendly URLs to phsyical paths">
<match url="^(.*)$" />
<action type="Rewrite" url="lang/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
所以如果我去'http://example.com/en/index' I need to open this page: 'http://example.com/lang/en/index.html'。
如何实现这个目标?
<rule name="the name" enabled="true" stopProcessing="true">
<match url="example.com/(.+)(?:/(.+)?)(.html)?"/>
<action type="Rewrite" url="lang/{R:1}/{R:2}.html"/>
</rule>
[更新] 终于解决了。这里是 Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="On" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Ignore" enabled="true" stopProcessing="true">
<match url="^(js|css).*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Redirect requests to friendly URLs">
<match url="^(.*?)/(.*)\.html$" />
<action type="Redirect" url="{R:2}" />
</rule>
<rule name="Rewrite friendly URLs to phsyical paths">
<match url="^\/(?!#.*).*" />
<action type="Rewrite" url="lang/{R:0}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
特别感谢 Ashkan Mobayen Khiabani 的 "Ignore" 部分(需要忽略:javaScript、图像和 CSS 文件夹以保持链接正常工作)