0

我如何重写一个 url,以便它在地址栏中显示类似 subdomain.example.com/blog 的路径,但显示一个类似 www.example.com/blog/?tag=/subdomain 的页面。

这是我想要发生的事件的过程:

第一个:我导航到 subdomain.example.com/blog 第二个:我被重定向到 www.example.com/blog/?tag=/subdomain 第三个:地址栏中的 url 仍然显示 subdomain.example.com/blog 即使我在 www.example.com/blog/?tag=/subdomain 页面。

我更喜欢使用 HttpContext.RewritePath() 方法

我一直试图在 IHttpModule 中对此进行编码但没有成功

这是我的代码:

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() { }

    }
    }
4

1 回答 1

1

如果您使用的是 IIS 7,则可以使用 IIS 7 URL Rewrite 扩展

于 2011-07-27T04:50:10.903 回答