Home
Gallery
GuestBook
SourceCode
Contact

UrlRewriting Woes

Posted in MPS
This post has been read 5594 times

After spending a very long time to build this site, i finally put it in production. The prime reason for the delay was hosting which i was not able to afford up until now.

Well everything seemed to work fine on my dev machine but once i put the code in production, my URL rewriting logic failed. I was using Gaidar Magdanurov's UrlRewiting solution. From what i observed the http module was not picking up on the application load. So as a last minute change i used UrlRewritingNet.UrlRewrite. I did not have to change much of the thing for this to work. Its pretty straight forward to setup and use.

First you have to add a config section


<configSections>

    <section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>

</configSections>

The next step is to define the http module ( This is where i think my other solution fails)

<httpModules>

    <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>

</httpModules>

Now you are ready to write the rules like so.

<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage="default.aspx" defaultProvider="RegEx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07">

    <rewrites>

        <add name="Post" virtualUrl="Post/(\d+)/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="ShowBlog.aspx?ID=$1" ignoreCase="true"/>

    </rewrites>

</urlrewritingnet>

Well you would guess that this is all you do and everything works fine. Even i thought so, but when i was testing this, i logged into my site and the URL which i expected to be something like this "http://vinay.qsh.eu/Post/1/something.aspx" had become "http://vinay.qsh.eu/Post/1/something.aspx?ID=1". This broke my page because there are 2 parameters with the same id. It beats me as to why this is being added on login/logout. It is not a postback issue as every other postback is working fine.

How to fix this? This is what i have done.

Add a helper method to check whether if the URL is rewritten one or plain URL

    1 public static bool URLRewritten(string LocalPath)

    2 {

    3     bool ruleFound = false;

    4     foreach (UrlRewritingNet.Configuration.RewriteSettings rewriteSettings in UrlRewritingNet.Web.UrlRewriting.Configuration.Rewrites)

    5     {

    6         UrlRewritingNet.Web.RewriteRule rewrite = null;

    7         string providerName = rewriteSettings.Provider;

    8         if ((string.IsNullOrEmpty(providerName)))

    9         {

   10             rewrite = UrlRewritingNet.Web.UrlRewriting.CreateRewriteRule();

   11         }

   12         else

   13         {

   14             rewrite = UrlRewritingNet.Web.UrlRewriting.CreateRewriteRule(providerName);

   15         }

   16         rewrite.Initialize(rewriteSettings);

   17 

   18 

   19         if ((rewrite.IsRewrite(LocalPath)))

   20         {

   21             ruleFound = true;

   22             break;

   23         }

   24     }

   25     return ruleFound;

   26 }

Then use this function in the LoggedIn and LoggedOut events to manage the URL

    1 protected void Login_LoggedIn(object sender, EventArgs e)

    2 {

    3     if (Helpers.URLRewritten(Request.Url.LocalPath))

    4     {

    5         if (Request.Url.PathAndQuery.Contains("ReturnUrl"))

    6             Response.Redirect(Request.Params["ReturnUrl"].ToString());

    7         else

    8             Response.Redirect(Request.Url.LocalPath);

    9     }

   10     else

   11     {

   12         if (Request.Url.PathAndQuery.Contains("ReturnUrl"))

   13             Response.Redirect(Request.Params["ReturnUrl"].ToString());

   14         else

   15             Response.Redirect(Request.Url.PathAndQuery);

   16     }

   17 }

   18 

   19 protected void LoginStatus1_LoggedOut(object sender, EventArgs e)

   20 {

   21     if (Helpers.URLRewritten(Request.Url.LocalPath))

   22     {

   23         Response.Redirect(Request.Url.LocalPath);

   24     }

   25     else

   26     {

   27         Response.Redirect(Request.Url.PathAndQuery);

   28     }

   29 }

One more thing. To manage the Return URL, you'll have to call the helper function like so

    1 if (Helpers.URLRewritten(Request.Url.LocalPath))

    2 {

    3     Response.Redirect(FormsAuthentication.LoginUrl +

    4                   "?ReturnUrl=" + Request.Url.LocalPath);

    5 }

    6 else

    7 {

    8     Response.Redirect(FormsAuthentication.LoginUrl +

    9                    "?ReturnUrl=" + Request.Url.PathAndQuery);

   10 }

I will be using this workaround until i find a fix.




Comment posted on Friday, April 30, 2010 11:48 PM
Hey, I realize it's been awhile since your post, but I just ran into this same problem and found what might be a simpler solution

string RedirectURL = "http://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.LocalPath;
            if (!(string.IsNullOrEmpty(Page.ClientQueryString)))
            {
                RedirectURL += "?" + Page.ClientQueryString;
            }

Comment posted on Saturday, May 01, 2010 1:13 AM
Actually, after screwing around for a little bit more, I found a better way to do it:

   string RedirectURL = (string.IsNullOrEmpty(HttpContext.Current.Request.QueryString.ToString())) ?
                HttpContext.Current.Request.Url.ToString()
                : HttpContext.Current.Request.Url.ToString().Replace(HttpUtility.UrlDecode(HttpContext.Current.Request.Url.Query),
                (string.IsNullOrEmpty(Page.ClientQueryString)) ? "" : "?" + Page.ClientQueryString);
          
            HttpContext.Current.Response.Redirect("/redirector.aspx?url=" + HttpUtility.UrlEncode(RedirectURL));
Comment posted on Saturday, May 01, 2010 1:29 AM
Actually, after screwing around for a little bit more, I found a better way to do it:

   string RedirectURL = (string.IsNullOrEmpty(HttpContext.Current.Request.QueryString.ToString())) ?
                HttpContext.Current.Request.Url.ToString()
                : HttpContext.Current.Request.Url.ToString().Replace(HttpUtility.UrlDecode(HttpContext.Current.Request.Url.Query),
                (string.IsNullOrEmpty(Page.ClientQueryString)) ? "" : "?" + Page.ClientQueryString);
          
            HttpContext.Current.Response.Redirect("/redirector.aspx?url=" + HttpUtility.UrlEncode(RedirectURL));
Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll