locked
Url re writing not work sometimes RRS feed

  • Question

  • User-1963923283 posted

    Hi Expert,


    I have created a  url rewriting module. its work 95%, but not work 5%.Here i'm using url.Segement to find our the id and then rewritepath to that id with original page.

    Here is my code,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    
    
    namespace UI.Modules
    {
        public class UrlReWriteModule : IHttpModule
        {
            #region IHttpModule Members
    
            public void Dispose()
            {
    
            }
    
            public void Init(HttpApplication context)
            {
                context.BeginRequest += new EventHandler(context_BeginRequest);
            }
    
            #endregion
    
    
            private void context_BeginRequest(object sender, EventArgs e)
            {
                HttpContext context = ((HttpApplication)sender).Context;
                string path = context.Request.Path.ToString();
                string url = context.Request.RawUrl.ToString();
    
    
                if (!url.Contains("ErrorPage.aspx"))
                {
    
                    if (url.Contains("Forums"))
                    {
                        ReWriteForums(context, url);
                    }
                }
    
            }
           
    
            private void ReWriteForums(HttpContext context, string url)
            {
                if (context.Request.Url.Segments.Length == 6) // detail view of the fourm
                {
                    string forumID = context.Request.Url.Segments[4].Trim().Replace("/","");
                    if (!string.IsNullOrEmpty(forumID))
                    {
                        context.RewritePath("~/Forums/ViewForum.aspx?ForumID=" + forumID);
                    }
                }
                if (context.Request.Url.Segments.Length == 5) // detail view of the fourm
                {
                    string cateslug = context.Request.Url.Segments[3].Trim();
                    string titelslug = context.Request.Url.Segments[4].Trim();
                    string fullslug = cateslug + titelslug;
                    string forumHID = DAO.GetFourmHeaderID(fullslug);
                    if (!string.IsNullOrEmpty(forumHID))
                    {
                        context.RewritePath("~/Forums/ViewForum.aspx?ForumID=" + forumHID);
                    }
                }
                else if (context.Request.Url.Segments.Length == 4) // http://localhost/home/forums/Csharp
                {
                    if (!context.Request.Url.ToString().Contains(".aspx"))
                    {
                        string cateslug = context.Request.Url.Segments[3].Trim();
                        string categorycode = DAO.GetCateCodeFromForumHeader(cateslug);
                        if (!string.IsNullOrEmpty(categorycode))
                        {
                            context.RewritePath("~/Forums/ListPosts.aspx?CateCode=" + categorycode);
                        }
                    }
    
                }
                else if (context.Request.Url.Segments.Length == 3)
                {
                    context.RewritePath("../Forums/ListPosts.aspx");
                }
            }
    
        }
    }
    


    Questions.

    1.if i use the url Segement , have any impact?

    2.any alternative ways to do kind of  task?

    thank you





    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using CodeGain.Utility;

    namespace CodeGain.Modules
    {
        public class UrlReWriteModule : IHttpModule
        {
            #region IHttpModule Members

            public void Dispose()
            {

            }

            public void Init(HttpApplication context)
            {
                context.BeginRequest += new EventHandler(context_BeginRequest);
            }

            #endregion

            private string extractArticleTitle(string url)
            {
                string title = string.Empty;

                title = url.Substring(url.LastIndexOf("/") + 1);

                return title;
            }

            private void context_BeginRequest(object sender, EventArgs e)
            {
                HttpContext context = ((HttpApplication)sender).Context;
                string path = context.Request.Path.ToString();
                string url = context.Request.RawUrl.ToString();


                if (!url.Contains("ErrorPage.aspx"))
                {

                    if (url.Contains("Articles"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("CodeSnippets"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("Tuorials"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("BookReview"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("Videos"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("General"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("Interviews"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("Jobs"))
                    {
                        BaseRewrite(context, url);
                    }

                    else if (url.Contains("KB"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("BestLinks"))
                    {
                        BaseRewrite(context, url);
                    }
                    else if (url.Contains("Forums"))
                    {
                        ReWriteForums(context, url);
                    }
                }

            }

            private void BaseRewrite(HttpContext context, string url)
            {
                if (!url.Contains(".aspx"))
                {

                    RouterToNoExtension(context, url);
                }
                else
                {
                    if (context.Request.Url.Segments.Length == 6) // view detail
                    {
                        string contenttype = context.Request.Url.Segments[2];
                        string category = context.Request.Url.Segments[3].Trim();
                        string subcategory = context.Request.Url.Segments[4].Trim();
                        string article = context.Request.Url.Segments[5].Trim();
                        string articleSlug = string.Concat(contenttype, category, subcategory, article);
                        RewriteView(context, articleSlug);
                    }
                }
            }

            private void RouterToNoExtension(HttpContext context, string url)
            {
                if (context.Request.Url.Segments.Length == 5) // view detail
                {

                }

                else if (context.Request.Url.Segments.Length == 4) // sub category
                {
                    // category redirection
                    string contenttype = string.Empty;
                    if (context.Request.UrlReferrer != null)
                    {
                        if (context.Request.UrlReferrer.Segments.Length >= 3)
                        {
                            contenttype = context.Request.UrlReferrer.Segments[2].Trim();
                        }
                    }
                    else
                    {
                        contenttype = context.Request.Url.Segments[2].Trim();
                    }
                    contenttype = contenttype.Replace("/", "");
                    string category = context.Request.Url.Segments[3].Trim();
                    category = category.Replace("/", "");
                    RewriteCategory(context, contenttype, category);
                }
                else if (context.Request.Url.Segments.Length == 3) // content type
                {
                    // content direction
                    string contentType = context.Request.Url.Segments[2].Trim();
                    contentType = contentType.Replace("/", "");
                    RewriteContent(context, contentType);
                }
                else
                {
                    string slug = context.Request.Url.Segments[1].Trim();
                    slug = slug.Replace("/", "");
                    if (slug.Equals("codegain"))
                    {
                        context.RewritePath("Home.aspx");
                    }
                }

                //int lasttopmostindex = url.LastIndexOf("/");
                //string suburl = url.Substring(0, lasttopmostindex);
                //int secondlastindex = suburl.LastIndexOf("/");
                //string slug = url.Substring(secondlastindex + 1, (lasttopmostindex - (secondlastindex + 1)));

                //if (slug.Equals("codegain"))
                //{
                //    context.RewritePath("home.aspx");

                //}
                //else
                //{
                //    RewriteContent(context, slug);
                //}
            }

            private void RewriteContent(HttpContext context, string slug)
            {

                if (slug.Equals("Jobs"))
                {
                    context.RewritePath("../Jobs/ListOfJobs.aspx");
                }
                else
                {
                    string paramcode = CodeGain.Utility.CodeGainUtilityDAO.GetParamCode(slug, "CONTYPE");
                    if (string.IsNullOrEmpty(paramcode))
                    {
                        paramcode = "00001";
                    }
                    context.RewritePath("~/Content/ViewList.aspx?" + ParamDefinition.CONTENTTYPE_PARAM + "=" + paramcode);
                }

            }

            private void RewriteCategory(HttpContext context, string content, string categoryslug)
            {

                string paramcode = CodeGain.Utility.CodeGainUtilityDAO.GetParamCode(content, "CONTYPE");
                string catecode = CodeGain.Utility.CodeGainUtilityDAO.GetCateCode(categoryslug);
                if (string.IsNullOrEmpty(paramcode))
                {
                    paramcode = "00001";
                }
                if (string.IsNullOrEmpty(catecode))
                {
                    catecode = "CG00001";
                }
                context.RewritePath("../Content/ViewList.aspx?" + ParamDefinition.CONTENTTYPE_PARAM + "=" + paramcode + "&" + ParamDefinition.CATEGORY_PARAM + "=" + catecode);

            }

            private void RewriteView(HttpContext context, string slug)
            {
                string articleID = CodeGainUtilityDAO.GetArticleID(slug);
                string actualPath = "~/Content/ViewDetail.aspx?artid=" + articleID;
                context.RewritePath(actualPath);
            }

            private void ReWriteForums(HttpContext context, string url)
            {
                if (context.Request.Url.Segments.Length == 6) // detail view of the fourm
                {
                    string forumID = context.Request.Url.Segments[4].Trim().Replace("/","");
                    if (!string.IsNullOrEmpty(forumID))
                    {
                        context.RewritePath("~/Forums/ViewForum.aspx?ForumID=" + forumID);
                    }
                }
                if (context.Request.Url.Segments.Length == 5) // detail view of the fourm
                {
                    string cateslug = context.Request.Url.Segments[3].Trim();
                    string titelslug = context.Request.Url.Segments[4].Trim();
                    string fullslug = cateslug + titelslug;
                    string forumHID = CodeGainUtilityDAO.GetFourmHeaderID(fullslug);
                    if (!string.IsNullOrEmpty(forumHID))
                    {
                        context.RewritePath("~/Forums/ViewForum.aspx?ForumID=" + forumHID);
                    }
                }
                else if (context.Request.Url.Segments.Length == 4) // http://localhost/codegain/forums/Csharp
                {
                    if (!context.Request.Url.ToString().Contains(".aspx"))
                    {
                        string cateslug = context.Request.Url.Segments[3].Trim();
                        string categorycode = CodeGainUtilityDAO.GetCateCodeFromForumHeader(cateslug);
                        if (!string.IsNullOrEmpty(categorycode))
                        {
                            context.RewritePath("~/Forums/ListPosts.aspx?CateCode=" + categorycode);
                        }
                    }

                }
                else if (context.Request.Url.Segments.Length == 3)
                {
                    context.RewritePath("../Forums/ListPosts.aspx");
                }
            }

        }
    }

    Tuesday, December 22, 2009 10:45 AM

Answers

  • User-1528972981 posted

    Hi,

    I recently worked on a similar requirement where I had to redirect a html site url to a sharepoint url. Because url rewriting is more of static predefined redirection, I have actually checked for few keywords and then based on that I have refined my url.

    I have not used segments in my case.

    for example

    if (filePath.Contains("contacts.html"))

    {
         filePath = filePath.Replace("contacts.html", "contacts/Pages/contacts.aspx");

    }

    finally

    //Redirecting to the new URL.
    HttpContext oContext = new HttpContext(httpContext.Request, httpContext.Response);
    oContext.Response.Redirect(filePath, false);

    Hope this helps

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 24, 2009 3:50 AM

All replies

  • User2130758966 posted

    One alternative way is to use the urlrewriter.net library.

    I have written a guide that explains how to setup something like this:

    The main urlrewriter project website has been down for the last few weeks though so I dont know if this project is dead or not but you can still get the files from sourceforge.


    If you are using asp.net 3.5 sp1 then you can also use the new Url Routing feature to do this kind of url rewriting.


    Tuesday, December 22, 2009 11:25 AM
  • User-1963923283 posted

    Hi


    Thank you for great help and your blog. but in my case i just gave forums on sample, but there i have 11 categories like forum, then how i can do using  urlwriter.


    Note:I have already use this lib, then i some issues, after i that i try componse own  module.any impact in my way, please help me.


    my complete is here


    #  private void context_BeginRequest(object sender, EventArgs e)  
    #         {  
    #             HttpContext context = ((HttpApplication)sender).Context;  
    #             string path = context.Request.Path.ToString();  
    #             string url = context.Request.RawUrl.ToString();  
    #   
    #   
    #             if (!url.Contains("ErrorPage.aspx"))  
    #             {  
    #   
    #                 if (url.Contains("Forums"))  
    #                 {  
    #                     ReWriteForums(context, url);  
    #                 }  
    else  if (url.Contains("articles"))  
    #                 {  
    #                     ReWritearticles(context, url);  
    #                 }  
    else  if (url.Contains("codesnippets"))  
     #                 {  
    #                     ReWritecodesnippets(context, url);  
    #                 }  
    #             }  
    #   
    #         }  


    Note:my orignal page is : same to every ccategory.(http:localhost/test/list.aspx?SecID= +1-fourms,2-articles,3-codesnippet

     
    i'm epxectin good help to improve my code or to do in any other ways thank you

    thank you



    Tuesday, December 22, 2009 8:23 PM
  • User-1963923283 posted

    Hi,


    Your sample project not wokring.its page not found


    thank you


    Tuesday, December 22, 2009 9:59 PM
  • User2130758966 posted

    Hi,

    Your sample project not wokring.its page not found

    thank you

    Hey, the projects official website at http://urlrewriter.net is down but I have just checked the demo application link at the footer and its working ok for me...

    Wednesday, December 23, 2009 6:54 AM
  • User2130758966 posted

    Note:my orignal page is : same to every ccategory.(http:localhost/test/list.aspx?SecID= +1-fourms,2-articles,3-codesnippet

     
    i'm epxectin good help to improve my code or to do in any other ways thank you

    thank you

    I have never done urlrewriting in this way sorry

    Wednesday, December 23, 2009 6:56 AM
  • User-1963923283 posted

    Hi Sir,


    thank you for your valueable information.


    are u think any impact using segement to do this as from your experience? if its how can i do this with regex.


    thank you

    Wednesday, December 23, 2009 12:02 PM
  • User-1528972981 posted

    Hi,

    I recently worked on a similar requirement where I had to redirect a html site url to a sharepoint url. Because url rewriting is more of static predefined redirection, I have actually checked for few keywords and then based on that I have refined my url.

    I have not used segments in my case.

    for example

    if (filePath.Contains("contacts.html"))

    {
         filePath = filePath.Replace("contacts.html", "contacts/Pages/contacts.aspx");

    }

    finally

    //Redirecting to the new URL.
    HttpContext oContext = new HttpContext(httpContext.Request, httpContext.Response);
    oContext.Response.Redirect(filePath, false);

    Hope this helps

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 24, 2009 3:50 AM
  • User-1963923283 posted

    Hi

    Thank you for your reply, in my case i'm taking url slug from the segment and then taking detail from the database to that slug and then rewrite path to orginal url. so in this case how i can do without segment?

    if segment good, have any security issues?

    thank you


    Sunday, December 27, 2009 9:53 PM
  • User2130758966 posted

    are u think any impact using segement to do this as from your experience?

    When you get it working today and its fresh in your head you wont have any problems by using the index of the segment.

    When you come back in the future you might forget what segment 2 means when you have three segments and what it means when you have four.


    If you wanted to extract it by a regex you could use named capture groups to form a kind of documentation for your code.


    Its fine to do it your way that you already have working, no sense in adding needless complexity. Just make sure you document it because you will forget when you come back to change it in a few months [:)]

    Tuesday, December 29, 2009 8:41 PM
  • User-1963923283 posted

    Hi

    Thank you once again. yes true i have created document to that part only. and i try add comments as well.

    thank you


    Tuesday, December 29, 2009 9:49 PM