2015-05-18 39 views
3

我想在我的asp.net多語言網站中使用帶規則部分的Web.config重寫URL。如何在多語言的ASP.NET網站中重寫URL

我有這樣的URL: http://example.com/lang/en/index.htmlhttp://example.com/lang/fr/index.html

我需要刪除lang.html擴展和重寫URL到: http://example.com/en/indexhttp://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'我需要打開此頁:'http://example.com/lang/en/index.html'。

如何實現這一目標?

回答

0

[已更新] 終於解決了。下面是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爲「忽略」部分(需要忽略:JavaScript,圖片和CSS文件夾,保持聯繫工作)

0
<rule name="the name" enabled="true" stopProcessing="true"> 
      <match url="example.com/(.+)(?:/(.+)?)(.html)?"/> 
      <action type="Rewrite" url="lang/{R:1}/{R:2}.html"/> 
</rule> 
+0

我想如果我打開http://example.com/en/index - 「您正在查找的資源已被刪除,名稱已更改,或者暫時不可用」 –

+0

會根據需要更改地址嗎?我不知道在哪裏example.com/en/index重定向我可以幫助你(和你要求的)是將example.com/lang/en/index改寫爲example.com/en/index –

+0

不,它不會。這個路徑:「/lang/en/index.html」它是物理文件夾和文件路徑。我只需要隱藏'lang'和'.html'。所以如果我去'http://example.com/en/index'它應該打開這個'http://example.com/lang/en/index.html' –