簡體   English   中英

URL重寫規則語法問題

[英]URL Rewrite Rule Syntax Question

我剛剛升級到VS2010 / IIS 7.5 / URL Rewrite 2.0。 我想做的事很簡單,但我真的很累,想讓它自己工作。

我只是想要干凈的 URL,使http://example.com/abc-def.aspx變為http://example.com/abc-def/ ,從而有效地刪除了.aspx擴展名並添加了斜杠。

我使用以下方法完成了此操作:

<rule name="Trim aspx for directory URLs">
    <match url="(.+)\.aspx$" />
    <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

這可以正常工作並按預期重定向,但不會拉出頁面,因此我認為我需要將其與Rewrite規則結合使用,以便它將干凈的URL解析為相應的.aspx頁面。

我試圖通過使用以下方法來做到這一點:

<rule name="Add aspx extension back internally">
    <match url="^http://example\.com/(.+)/$" ignoreCase="true" />
    <conditions>
        <add input="{URL}" matchType="IsDirectory" negate="true" />
        <add input="{URL}" pattern=".+/externals/.+" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

重定向規則有效,但似乎內部重寫規則不起作用,因為頁面沒有上拉。 我究竟做錯了什么?

不太確定URL Rewrite 2.0是否可以同時執行以下兩種操作:

  1. 刪除.aspx擴展名,並在傳入的請求上添加斜杠/。
  2. 在內部添加.aspx擴展名,以便加載正確的頁面,而不是獲取404。

我決定要做的是在源代碼中的任何地方進行更改,以指向無.aspx擴展名的URL,這樣就永遠不會出現以.aspx結尾的URL的外部請求。

那讓我只需要:

<rule name="Add aspx extension back internally" stopProcessing="true">
    <match url="(.+)/$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

<rule name="Add trailing slash" stopProcessing="false">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{URL}" pattern="favicon\.ico" ignoreCase="true" negate="true" />
        <add input="{URL}" pattern="\.axd" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}/" />
</rule>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM