iis - 一个重定向来统治他们

标签 iis redirect url-rewriting seo url-redirection

尝试help a fellow developer之后在 iis.net 论坛上,我开始寻找一种更好的重定向方法。因此我得到了 this article.

我采纳了很多这些想法并开始实现我自己的版本。一切似乎都很好,但我遇到了一个障碍,我希望你能帮忙。

好的,快速解释一下我的规则:

我的想法是测试 url,如果我发现它有问题,我会重写 url 并让它继续,而不是立即重定向它。如果我在任何时候重写 url,我都会将自定义服务器变量“Redirect”设置为 true。最后,我测试我的自定义服务器变量是否为真并重定向用户。

它的重点是只有一个 301 重定向,而不是一连串的重定向。

这些是我的规则(对不起墙):

<rules>
  <rule name="WhiteList - resources" stopProcessing="true">
    <match url="^resources/" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="None" />
  </rule>

  <rule name="Redirect subdomains with www to non-www" stopProcessing="false">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
      <add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" />
    </conditions>
    <action type="Rewrite" url="http://{C:1}.{C:2}.{C:3}{HTTP_URL}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="Redirect top domains with non-www to www" stopProcessing="false">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
      <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
    </conditions>
    <action type="Rewrite" url="http://www.{HTTP_HOST}{HTTP_URL}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>

  <rule name="SEO - Remove trailing slash" stopProcessing="false">
    <match url="(.*)/$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - ToLower" stopProcessing="false">
    <match url="(.*)" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
      <add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" />
      <add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" />
    </conditions>
    <action type="Rewrite" url="{ToLower:{R:1}}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - remove default.aspx" stopProcessing="false">
    <match url="(.*?)/?default\.aspx$" />
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - Trim aspx" stopProcessing="false">
    <match url="(.*)\.aspx$" />
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>

  <rule name="SEO - non-canonical redirect" stopProcessing="true">
    <match url="^(.*)" />
    <conditions>
      <add input="{Redirect}" pattern="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="false" />
    </serverVariables>
  </rule>
</rules>

现在大部分实际上工作得很好,但我似乎对裸域有一些问题。

如果我使用带有 www 的子域(应该重定向到非 www),那么它似乎会失败。有趣的是,如果我转到一个子页面,它工作正常。

(我不能发布更多的 url,因为我是新来的 :( 我想感谢其他人让我开始做这件事。)

我已经将它追溯到服务器变量 {HTTP_URL} 因为如果我删除它,它不会失败。然而,它当然也没有做它应该做的事情(它总是重定向到裸域)。我尝试了各种变量:{URL}{REQUEST_URI},它们似乎都以相同的错误结束,这有点烦人。

如果有人对规则有任何改进,请随时回复,我喜欢和他们一起工作,我想做出近乎完美的重定向,所以欢迎任何建议。

最佳答案

这最终更像是版本 1,此后我对其进行了改进以避免服务器变量。 现在我使用一系列重写规则将 url 转换为我喜欢的东西,附加下划线。 然后剥离下划线,然后重定向。 它对我来说已经运行了很长一段时间,而且公司几乎在所有项目中都在使用它。 它已被制作成一个 nuget 包,以便于设置。 http://www.nuget.org/packages/RedirectRules/

关于iis - 一个重定向来统治他们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707535/

相关文章:

url-rewriting - 如何重定向 - 将 url 重写为短 url

iis - 在 VirtualBox IIS 上访问本地主机

c# - 无法远程返回自定义 HTTP 错误详细信息

.htaccess - 站点地图中不再存在的页面需要重定向

c - 使用子进程和父进程写入管道

scala 和 lift : rewriting URL using variable declared outside LiftRules. statelessRewrite.append

c# - 错误无法使用 appcmd (ASP.Net) 找到具有站点名称标识符的站点对象

iis - VS2017/Angular 7升级部署错误(开发模式)

php - 重定向到 WordPress 中的登录页面

regex - "%"字符导致 IIS URL 重写正则表达式出现问题