IIS 重写和重定向规则在我的网站上不起作用
IIS rewrite and redirect rules doesn't work on my site
您好,在 GitHub 页面上托管的网页前使用 IIS 重写和重定向规则有两个问题。
重写到GitHub页
更新:一切正常。如本问题后面所述,是 GitHub 页面重定向到规范 URL。在点击 GitHub 页面之前添加一个重写附加缺失的尾部斜杠修复它:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
该站点托管在 GitHub 个页面上,可在此处访问:http://elmahio.github.io/blog/
。
为了重写对我的代理的请求,我添加了以下重写规则:
<rule name="Proxy" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^(.*)/.well-known/(.*)$"/>
</conditions>
<action type="Rewrite" url="http://elmahio.github.io/blog/{R:1}" />
</rule>
重写规则很有魅力。但是,如果有人忘记了 URL 的尾部斜杠,它似乎会重定向到 GitHub 页面版本。示例:
https://blog.elmah.io/great-dot-net-conferences-to-attend
重定向到 http:// elmahio.github.io/blog/great-dot-net-conferences-to-attend/
我可以看到 GitHub 页面会自动附加尾部斜杠(如果没有)。这甚至可能是主要问题,因为我的代理重写为 GitHub 一个,然后重定向以附加尾部斜杠。
关于如何解决这个问题有什么想法吗?也许在我的代理中添加重定向规则,在重写到 GitHub 页面之前附加斜杠?
重定向到 HTTPS
更新:删除我的自定义重定向规则并在 Azure 上的 自定义域 视图上启用 仅 HTTPS,已修复这个问题
我希望所有对非 https URL 的请求都重定向到 HTTPS。我已将重定向规则添加到 web.config
,如下所示:
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent"/>
</rule>
重写规则在 URL 包含路径时工作正常。示例:
http://blog.elmah.io/great-dot-net-conferences-to-attend/ redirects to https://blog.elmah.io/great-dot-net-conferences-to-attend/
但是在没有 HTTPS 的情况下访问首页 (http://blog.elmah.io/ or http://blog.elmah.io) 时,我没有被重定向到 HTTPS URL。
这里会出什么问题?
您的所有重写都是正确的。由于 GitHub 页,很可能存在问题。尝试执行以下操作:
1) 创建一个规则以附加尾部斜杠。您可以在此线程中找到重写规则的示例 IIS URL Rewrite: Add trailing slash except for .html and .aspx
2) 您可以在 Azure 上的自定义域视图上启用仅 HTTPS,而不是使用重写规则重定向到 HTTPS
您好,在 GitHub 页面上托管的网页前使用 IIS 重写和重定向规则有两个问题。
重写到GitHub页
更新:一切正常。如本问题后面所述,是 GitHub 页面重定向到规范 URL。在点击 GitHub 页面之前添加一个重写附加缺失的尾部斜杠修复它:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
该站点托管在 GitHub 个页面上,可在此处访问:http://elmahio.github.io/blog/
。
为了重写对我的代理的请求,我添加了以下重写规则:
<rule name="Proxy" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^(.*)/.well-known/(.*)$"/>
</conditions>
<action type="Rewrite" url="http://elmahio.github.io/blog/{R:1}" />
</rule>
重写规则很有魅力。但是,如果有人忘记了 URL 的尾部斜杠,它似乎会重定向到 GitHub 页面版本。示例:
https://blog.elmah.io/great-dot-net-conferences-to-attend
重定向到 http:// elmahio.github.io/blog/great-dot-net-conferences-to-attend/
我可以看到 GitHub 页面会自动附加尾部斜杠(如果没有)。这甚至可能是主要问题,因为我的代理重写为 GitHub 一个,然后重定向以附加尾部斜杠。
关于如何解决这个问题有什么想法吗?也许在我的代理中添加重定向规则,在重写到 GitHub 页面之前附加斜杠?
重定向到 HTTPS
更新:删除我的自定义重定向规则并在 Azure 上的 自定义域 视图上启用 仅 HTTPS,已修复这个问题
我希望所有对非 https URL 的请求都重定向到 HTTPS。我已将重定向规则添加到 web.config
,如下所示:
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent"/>
</rule>
重写规则在 URL 包含路径时工作正常。示例:
http://blog.elmah.io/great-dot-net-conferences-to-attend/ redirects to https://blog.elmah.io/great-dot-net-conferences-to-attend/
但是在没有 HTTPS 的情况下访问首页 (http://blog.elmah.io/ or http://blog.elmah.io) 时,我没有被重定向到 HTTPS URL。
这里会出什么问题?
您的所有重写都是正确的。由于 GitHub 页,很可能存在问题。尝试执行以下操作:
1) 创建一个规则以附加尾部斜杠。您可以在此线程中找到重写规则的示例 IIS URL Rewrite: Add trailing slash except for .html and .aspx
2) 您可以在 Azure 上的自定义域视图上启用仅 HTTPS,而不是使用重写规则重定向到 HTTPS