重定向 URL 中的第二个尾部斜杠

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

我正在使用 URL 重写来强制 http 到 https 重定向。它正在工作,只是它在 URL 中添加了第二个尾部斜杠。例如,如果我访问 http://example.com,重写的 URL 最终会是 https://example.com//。我什至不想要 1 个尾部斜杠,更不用说 2 个,而且我无法摆脱它。我尝试了this解决方案,但没有成功。可能是因为这个人想去掉结尾的斜杠,而我有两个斜杠,所以它不匹配?

我的 web.config 文件中有以下规则。我正在运行带有 IIS 10 和 URL 重写模块的 Windows Server 2019。如果有人能告诉我哪里出错了,我将不胜感激!

<rule name="HTTPS Redirect" stopProcessing="true">
    <match url="(.*)" />
        <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="SEO - Remove trailing slash" stopProcessing="false">
    <match url="(.+)/$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
    <action type="Rewrite" url="_{R:1}" />
</rule>
iis url-rewriting iis-10
3个回答
5
投票

只需删除操作重定向 URL 中 {HTTP_HOST} 和 {REQUEST_URI} 之间的正斜杠即可。

https://serverfault.com/questions/893315/best-way-to-redirect-all-http-to-https-in-iis


1
投票

您可以尝试使用此 URL 重写规则:

<rule name="HTTPS Redirect" stopProcessing="true">
    <match url="^(.*)$" />
        <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="SEO - Remove trailing slash" stopProcessing="false">
    <match url="(.+)/$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
    <action type="Rewrite" url="_{R:1}" />
</rule>

0
投票

我已经解决了这个问题,我注意到这个问题中给出的其他答案不起作用,所以我解决了这个问题,并想分享对我有用的内容。请找到下面的重写规则,该规则正在从 url 中删除双斜杠。我们需要将其放入 web.config 中。

<rewrite>
  <rules>
    <rule name="RemoveDoubleSlash" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="(.*)" />
        <action type="Redirect" url="{C:1}/{C:2}" />
        <conditions>
            <add input="{UNENCODED_URL}" pattern="(.*)//(.*)" />
        </conditions>
    </rule>
  </rules>
</rewrite>
© www.soinside.com 2019 - 2024. All rights reserved.