在 web.config 中包含斜线的 URL 重定向不起作用

问题描述 投票:0回答:1

我目前设置了以下 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}&amp;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}&amp;subNav={R:2}&amp;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}&amp;subNav={R:2}&amp;subsubNav={R:3}&amp;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}&amp;subNav={R:2}&amp;subsubNav={R:3}&amp;subsubsubNav={R:4}&amp;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> 
url redirect iis http-redirect
1个回答
0
投票

这应该可以解决问题:

<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>

来源:Web.config 使用重写规则进行重定向 - https、www 等

© www.soinside.com 2019 - 2024. All rights reserved.