我目前设置了以下 web.config 来重写 url,但出于 SEO 目的,客户希望在每个 url 末尾添加反斜杠。有没有更好的方法来编写下面的内容以合并斜杠,或者是否可以添加一条规则来在重写 url 后添加斜杠?我已经尝试过这里的建议,但没有用:
https://www.rewriteguide.com/iis-add-remove-trailing-slash/
<rule name="RewriteUserFriendlyURL" enabled="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="submit.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="index.cfm?topNav={R:1}" />
</rule>
<rule name="RewriteUserFriendlyURL2" enabled="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="submit.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="index.cfm?topNav={R:1}&subNav={R:2}" />
</rule>
<rule name="RewriteUserFriendlyURL3" enabled="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="submit.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="index.cfm?topNav={R:1}&subNav={R:2}&subsubNav={R:3}" />
</rule>
<rule name="RewriteUserFriendlyURL4" enabled="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="submit.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="index.cfm?topNav={R:1}&subNav={R:2}&subsubNav={R:3}&subsubsubNav={R:4}" />
</rule>
<rule name="RewriteUserFriendlyURL5" enabled="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="submit.cfm" negate="true" />
</conditions>
<action type="Rewrite" url="index.cfm?topNav={R:1}&subNav={R:2}&subsubNav={R:3}&subsubsubNav={R:4}&subsubsubsubNav={R:5}" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
这应该可以解决问题:
<rule name="AddTrailingSlash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>