Adding a trailing slash to all URLs through IIS URL Rewrite Module is widely spread, but how do I add exceptions for URLs that ends with .html and .aspx?
通过IIS URL重写模块向所有URL添加尾部斜杠广泛传播,但如何为以.html和.aspx结尾的URL添加例外?
Today I have this:
今天我有这个:
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<!-- Doesn't seem to be working -->
<!--<add input="{REQUEST_URI}" pattern="(.*?).html$" negate="true" />-->
<!--<add input="{REQUEST_URI}" pattern="(.*?).aspx$" negate="true" />-->
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
5 个解决方案
#1
22
If you want something done right, you've got to do it yourself, obviously...
如果你想做正确的事情,你必须自己做,显然......
Here is the solution to my question:
这是我的问题的解决方案:
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Update: I blogged about this in more detail.
更新:我在博客中详细介绍了这一点。
#2
13
Varying the other answers, I used this so I wouldn't have to specify a list of file extensions:
改变其他答案,我使用了这个,所以我不必指定文件扩展名列表:
<!-- Ensure trailing slash -->
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
#3
2
We add multiple extensions like this:
我们添加了多个这样的扩展:
<add input="{URL}" negate="true" pattern="((.+).(jpg|ico|gif|js|png|htm|css|html))" ignoreCase="true" />
#4
1
To prevent all files from having a slash added, I changed the match rule to this:
为了防止所有文件都添加了斜杠,我将匹配规则更改为:
<match url="^([^.]*[^/])$" />
That applies the rule only to paths that include any number of non-dot characters that does not end in a slash. So any path that includes a dot (e.g. xxx.html, xxx.aspx, etc.) would be excluded without needing any additional negation rule.
这仅将规则应用于包含任何不以斜杠结尾的非点字符的路径。因此,任何包含点(例如xxx.html,xxx.aspx等)的路径都将被排除,而不需要任何其他否定规则。
Looking for the presence of a dot in the match rule allowed me to completely remove the condition rules that use match types IsFile and IsDirectory. Those match types are only allowed in distributed rules (web.config), not in the global rules (applicationHost.config), so I had been forced to replicate this rule for every site instead of applying it to all sites using a global rule. By modifying the regex in the match rule to exclude files and removing the IsFile and IsDirectory conditions, I was able to create a global rule instead of having multiple distributed rules.
在匹配规则中查找点的存在允许我完全删除使用匹配类型IsFile和IsDirectory的条件规则。这些匹配类型仅在分布式规则(web.config)中允许,而不是在全局规则(applicationHost.config)中,因此我*为每个站点复制此规则,而不是使用全局规则将其应用于所有站点。通过修改匹配规则中的正则表达式来排除文件并删除IsFile和IsDirectory条件,我能够创建一个全局规则而不是具有多个分布式规则。
#5
0
This almost worked for me. I had to change it to
这几乎对我有用。我不得不改变它
<add input="{URL}" pattern="(.*?)\.html$" negate="true" />
<add input="{URL}" pattern="(.*?)\.aspx$" negate="true" />
Otherwise thanks for this!
否则,谢谢!
#1
22
If you want something done right, you've got to do it yourself, obviously...
如果你想做正确的事情,你必须自己做,显然......
Here is the solution to my question:
这是我的问题的解决方案:
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Update: I blogged about this in more detail.
更新:我在博客中详细介绍了这一点。
#2
13
Varying the other answers, I used this so I wouldn't have to specify a list of file extensions:
改变其他答案,我使用了这个,所以我不必指定文件扩展名列表:
<!-- Ensure trailing slash -->
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
#3
2
We add multiple extensions like this:
我们添加了多个这样的扩展:
<add input="{URL}" negate="true" pattern="((.+).(jpg|ico|gif|js|png|htm|css|html))" ignoreCase="true" />
#4
1
To prevent all files from having a slash added, I changed the match rule to this:
为了防止所有文件都添加了斜杠,我将匹配规则更改为:
<match url="^([^.]*[^/])$" />
That applies the rule only to paths that include any number of non-dot characters that does not end in a slash. So any path that includes a dot (e.g. xxx.html, xxx.aspx, etc.) would be excluded without needing any additional negation rule.
这仅将规则应用于包含任何不以斜杠结尾的非点字符的路径。因此,任何包含点(例如xxx.html,xxx.aspx等)的路径都将被排除,而不需要任何其他否定规则。
Looking for the presence of a dot in the match rule allowed me to completely remove the condition rules that use match types IsFile and IsDirectory. Those match types are only allowed in distributed rules (web.config), not in the global rules (applicationHost.config), so I had been forced to replicate this rule for every site instead of applying it to all sites using a global rule. By modifying the regex in the match rule to exclude files and removing the IsFile and IsDirectory conditions, I was able to create a global rule instead of having multiple distributed rules.
在匹配规则中查找点的存在允许我完全删除使用匹配类型IsFile和IsDirectory的条件规则。这些匹配类型仅在分布式规则(web.config)中允许,而不是在全局规则(applicationHost.config)中,因此我*为每个站点复制此规则,而不是使用全局规则将其应用于所有站点。通过修改匹配规则中的正则表达式来排除文件并删除IsFile和IsDirectory条件,我能够创建一个全局规则而不是具有多个分布式规则。
#5
0
This almost worked for me. I had to change it to
这几乎对我有用。我不得不改变它
<add input="{URL}" pattern="(.*?)\.html$" negate="true" />
<add input="{URL}" pattern="(.*?)\.aspx$" negate="true" />
Otherwise thanks for this!
否则,谢谢!