【问题标题】:Regular expression to Redirect from IIS从 IIS 重定向的正则表达式
【发布时间】:2014-04-07 10:56:41
【问题描述】:

我是正则表达式的新手。现在我正在编写一个重写规则来重定向页面。我有一个网址是

http://abc dot com/blog/social-media-page/

现在我必须通过 IIS 将上面的 url 重定向到这个 url。

http://blog dot abc dot com/social-media-page

如何通过正则表达式处理。

【问题讨论】:

  • 'abc dot com'还是abc.com
  • 是只有这个页面,还是之前域中的所有页面都到新的虚 URL?我问的原因是这可以通过 IIS 作为 301 重定向来实现。
  • 所有包含/blog的url都应该通过IIS重定向重定向到blog.abc.com
  • 你能告诉我如何使用 IIS 重定向来实现这一点吗?因为它需要正则表达式

标签: regex iis redirect


【解决方案1】:

搜索这个:

http:\/\/([^\/]+)\/([^\/]+)

并用这个替换:

http://\2 dot \1

这里的正则表达式演示:http://regex101.com/r/jZ4vC2

【讨论】:

  • 如何在 IIS 中编写你的正则表达式
  • 实际上是 .com 而不是 .com
【解决方案2】:

您可以使用反向引用:

'http:\/\/([a-z]+)\s([a-z]+)\s([a-z]+)\/([^\/]*)'

并将匹配替换为

'http://\4 dot \1 dot \3'

演示

http://regex101.com/r/rE8lP6

【讨论】:

    【解决方案3】:

    我的问题是这样的,谷歌已经用结构索引了我的页面 http://www.donbalon.com/noticia/detalle/4623/ES/descubren-en-argentina-los-secretos-mejor-guardados-de-leo-messi 我想做一个 301 重定向到相同的 URL,但没有“ES”参数,即 http://www.donbalon.com/noticia/detalle/4623/descubren-en-argentina-los-secretos-mejor-guardados-de-leo-messi 。在 web.config 文件中,我有以下内容,但无法正确重定向。 任何人都知道如何从规则 web.config 文件中做到这一点?

    <rule name = "RedirectWithoutIsoCode" stopProcessing = "true">
                       <match url = ". *" />
                       <conditions>
                           <add input = "{HTTP_HOST}" pattern = "^http:\/\/www\.donbalon\.com\/noticias\/detalle\/(\d+)\/([A-Z]+)\/([a-zA-Z0-9\-\.]+)"/>
                       </conditions>
                       <action type="Redirect" url="www.donbalon.com/detalle/noticia/{R:1}/{R:3}" redirectType="Permanent" />
                   </rule>

    谢谢

    编辑

    最后我在 global.asax 中使用了 protected void Application_PreRequestHandlerExecute (Object sender, EventArgs e)。使用正则表达式,我检测到我想要删除的文本部分,并在没有该部分的情况下构建了新的 URL。然后我对当前请求使用 301 重定向响应对象

            protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        {
            Regex expresionDetalle = new Regex(@"/([a-z]+)/detalle/(\d+)/([A-Z]{2})/([a-zA-Z0-9\-\.]*)");
    
            Regex expresionCodigoIso = new Regex(@"ES|AR|GT|EC|US|VE|UY");
            string[] partesURL;
            StringBuilder nuevaURL;
    
            string currentUrl = HttpContext.Current.Request.Path;
    
            if (expresionDetalle.IsMatch(currentUrl))
            {
                partesURL = currentUrl.Split('/');
                nuevaURL = new StringBuilder();
                foreach (string s in partesURL)
                {
                    if (!expresionCodigoIso.IsMatch(s) &&
                        !string.IsNullOrEmpty(s))
                    {
                        nuevaURL.Append('/');
                        nuevaURL.Append(s);
                    }
                }
    
                Response.Status = "301 Moved Permanently";
                Response.RedirectPermanent(nuevaURL.ToString());
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2018-03-14
      • 2013-07-25
      • 1970-01-01
      • 2023-03-15
      • 2023-01-29
      • 2018-04-19
      相关资源
      最近更新 更多