Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Pollack 1 post 21 karma points
    Jul 28, 2011 @ 16:34
    Dan Pollack
    0

    Conditional URL re-writing

    Hello, I am new to this forum. I am trying to do a conditional URL re-write (the user will be redirected to http://{site}/{web or mobile or text}/{state}/{county} in Umbraco using the Urlrewriting.config and possibly the Umbraco C# Razor API. I know how to do Url re-writing, but for the life of me cannot figure out how to do conditional re-writes based on the client (user's OS). I'm trying to do this without using any type of re-directing at all whatsoever. Any thoughts?

     

     

    I added the following configuration node to the Urlrewriting.config file.

     

    <add name="countyrewrite"

          virtualUrl="^~/(.*)/(.*)/\?(.*)"

          rewriteUrlParameter="IncludeQueryStringForRewrite"

          rule.Rewrite = "RewriteOption.Application"

          destinationUrl="~/web.aspx?state=$1&amp;county=$2&amp;$3"

          ignoreCase="true" />

     

    This works to redirect to the web.aspx page. However, there needs to be a way for the user agent to determine the OS platform.

    I attempted to use the UrlRewritingNet library and create a “Browser Redirect” Scripting file. This file is written with Razor and attempts to re-write the above rule based on the platform detected. See Below:

     

     if (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" ) 
            {
                
    RegExRewriteRule rule;
                
    rule = new RegExRewriteRule();
                
    rule.VirtualUrl = @"^~/(.*)/(.*)/\?(.*)";
                
    rule.DestinationUrl = "~/mobile.aspx?state=$1&amp;county=$2&amp;$3";
                
    rule.IgnoreCase = true;
                
    rule.Rewrite = RewriteOption.Application;
                
    rule.RewriteUrlParameter = RewriteUrlParameterOption.IncludeQueryStringForRewrite;
                
    UrlRewriting.ReplaceRewriteRule("countyrewrite", rule);
            }
            
    else 
            {
            
                
    RegExRewriteRule rule;
                
    rule = new RegExRewriteRule();
                
    rule.VirtualUrl = @"^~/(.*)/(.*)/\?(.*)";
                
    rule.DestinationUrl = "~/web.aspx?state=$1&amp;county=$2&amp;$3";
                
    rule.IgnoreCase = true;
                
    rule.Rewrite = RewriteOption.Application;
                
                
    rule.RewriteUrlParameter = RewriteUrlParameterOption.IncludeQueryStringForRewrite;
                
    UrlRewriting.ReplaceRewriteRule("countyrewrite", rule);
            }      

     

    I had a hunch that this would not work because of the page loading sequence. I have some ideas involving Server.Transfer, but maybe there are some other options.

  • Waylon Martinez 12 posts 37 karma points
    Jul 29, 2011 @ 02:37
    Waylon Martinez
    0

    You can easily create your own custom urlrewriting provider - they are very easy to make then you will have the ability to do something like this.

     

    <urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07" rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString"  >
      <providers>
        <add name="BrowserTypeProvider" type="YourNamespace.BrowserTypeProvider, YourAssembly"/>
       </providers>

    <add name="RewriteByBrowerType"
         provider="
    BrowserTypeProvider"
          virtualUrl="^~/(.*)/(.*)/\?(.*)"
         rewriteUrlParameter="ExcludeFromClientQueryString"
          destinationUrl="/{0}.aspx?state=$1&amp;county=$2&amp;$3"
          ignoreCase="true" encoding="true" 

    />

     

     

     

     

    public class BrowserTypeProvider: UrlRewritingProvider
        {
            public static void SetFormPostbackOverrideTo(string overridePostBackUrl)
            {
                var context = HttpContext.Current;
                if(context.Items.Contains(UrlRewrittingActionOverrideContextKey))
                {
                    context.Items.Remove(UrlRewrittingActionOverrideContextKey);
                   
                }
               
                    context.Items.Add(UrlRewrittingActionOverrideContextKey,overridePostBackUrl);
               
            }


            public override RewriteRule CreateRewriteRule()
            {
                var context = HttpContext.Current;
                return new BrowserTypeProviderRule(context);
            }
        }

        public class BrowserTypeProviderRule: RegExRewriteRule
        {
            private Regex regex;
            privat HttpContext context;

            public BrowserTypeProviderRule(HttpContext context)
            {
               this.context = context;
                UrlHelper urlHelper = new UrlHelper();
                if (IgnoreCase)
                {
                    regex = new Regex(urlHelper.HandleRootOperator(VirtualUrl),
                    RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions);
                }
                else
                {
                    regex = new Regex(urlHelper.HandleRootOperator(VirtualUrl),
                    RegexOptions.Compiled | RegexOptions);
                }
            }

            public override string RewriteUrl(string url)
            {
               var page= (context.Request.Browser["IsMobileDevice"] == "true" || context.Request.Browser["BlackBerry"] == "true") ? "mobile" : "web";

    DestinationUrl = String.Format(DestinationUrl, page); //since we added the "{0}" in the destination url this will allow you to use the string.format

    var rUrl = Regex.Replace(url, VirtualUrl, delegate(Match match)
                                                        {
                                                            string returnUrl = DestinationUrl;

                                                            for (int i = 1; i < match.Groups.Count; i++)
                                                            {
                                                                returnUrl = Regex.Replace(returnUrl, "(\\" + "$" + i + "\\b)", HttpUtility.UrlEncode(match.Groups[i].ToString().Replace("+", " "))); //Note: Added the .Replace("+", " ") because it seems that when a request came in with a converted " " to "+" the server wouldn't resolve the request
                                                            }

                                                            return returnUrl;
                                                        });


                return  rUrl;
            }

        
        }
    }

     

     

    Hopefully this will work as i described it - i typed the code into the WYSIWYG so therer might be some typeos and possible code errors - i appologize in advance.

     

    I would love to see if that worked for you.

     

     

  • Waylon Martinez 12 posts 37 karma points
    Jul 29, 2011 @ 02:41
    Waylon Martinez
    0

    Of course just incase it isn't implied you will have to create your own code assembly to house this example then move the library into the bin.

  • Waylon Martinez 12 posts 37 karma points
    Aug 12, 2011 @ 18:12
    Waylon Martinez
    0

    @Dan Pollack Did that work?

Please Sign in or register to post replies

Write your reply to:

Draft