开发者

Url Rewriting in C# .net

开发者 https://www.devze.com 2023-03-23 08:12 出处:网络
how can i rewrite a url so that it shows a path like subdomain.example.com/blog in the address bar but displays a page such as www.example.com/blog/?tag=/subdomain.

how can i rewrite a url so that it shows a path like subdomain.example.com/blog in the address bar but displays a page such as www.example.com/blog/?tag=/subdomain.

here is the process of events I want to occur:

first: I navigate to subdomain.example.com/blog Second: I 开发者_如何学Goam redirected to www.example.com/blog/?tag=/subdomain Third: the url in the address bar still displays subdomain.example.com/blog even though I am at the www.example.com/blog/?tag=/subdomain page.

I would prefer to use HttpContext.RewritePath() method

I have been trying to code this in a IHttpModule without success

here is my code:

using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;

namespace CommonRewriter
{
    public class ParseUrl : IHttpModule
    {
        public ParseUrl()
        {

        }

        string req = null;
        string rep = null;

        public String ModuleName
        {
            get { return "CommonRewriter"; }
        }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);
            application.EndRequest += new EventHandler(application_EndRequest);
            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
        }

        void application_AuthorizeRequest(object sender, EventArgs e)
        {

        }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {


        }

        private string ParseAndReapply(string textToParse)
        {
            string final = null;

            if (textToParse.Contains("example.com"))
            {
                string[] splitter = textToParse.Split('.');

                if (splitter[0].ToLower() != "www" && (splitter[2].ToLower()).Contains("blog"))
                {
                    string add = splitter[0].Remove(0, 7);
                    final = ("http://www.example.com/blog/?tag=/" + add);
                }
                else { final = textToParse; }
            }
            else { final = textToParse; }

            return final;
        }

        void application_BeginRequest(object sender, EventArgs e)
        {
            req = HttpContext.Current.Request.Url.AbsoluteUri;

            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;


            if (req.ToLower().Contains("example.com/blog") && !req.ToLower().Contains("www."))
            {
                string[] split = req.Split('.');
                if (split[1] == "example")
                {
                    rep = ParseAndReapply(req);

                    context.RewritePath(rep);

                    context.Response.End();
                }

            }
        }


        void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpContext context1 = HttpContext.Current;

            if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("/?tag=/")) 
            {
                context.RewritePath(req,false);
            }

            }
        }

        public void Dispose() { }

    }
    }


If you are using IIS 7, you can use the IIS 7 URL Rewrite extension

0

精彩评论

暂无评论...
验证码 换一张
取 消