正则表达式从IIS重定向

时间:2014-04-07 10:56:42

标签: regex iis redirect

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

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

现在我必须通过IIS将以上网址重定向到此网址。

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

我如何通过正则表达式处理这个问题。

3 个答案:

答案 0 :(得分:0)

搜索:

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

并替换为:

http://\2 dot \1

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

答案 1 :(得分:0)

您可以使用反向引用:

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

并将匹配替换为

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

演示

http://regex101.com/r/rE8lP6

答案 2 :(得分:0)

我的问题是,Google已将我的网页编入索引  http://www.donbalon.com/noticia/detalle/4623/ES/descubren-en-argentina-los-secretos-mejor-guardados-de-leo-messi我希望301重定向到相同的网址,但没有" 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>
&#13;
&#13;
&#13;

由于

修改

最后,我在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());
        }
    }