Exemplo n.º 1
0
 /// <summary>
 /// 重写URL
 /// </summary>
 /// <param name="context">HttpContext对象</param>
 /// <param name="dirPath">站点目录</param>
 /// <param name="requestPath">获取的URL地址</param>
 private void RewriteUrl(HttpContext context, string dirPath, string requestPath, string requestPage)
 {
     foreach (Model.url_rewrite url in SiteUrls.GetSiteUrls().Urls)
     {
         if ((Regex.IsMatch(requestPath, "^" + dirPath + url.pattern + "$", RegexOptions.None | RegexOptions.IgnoreCase)) && url.type != "no")
         {
             string newUrl = Regex.Replace(requestPath, dirPath + url.pattern, url.querystring, RegexOptions.None | RegexOptions.IgnoreCase);
             context.RewritePath(dirPath + DTKeys.DIRECTORY_REWRITE_ASPX + "/" + url.page, string.Empty, newUrl);
             return;
         }
     }
     if (IsAspxFile(requestPath))
     {
         context.RewritePath(dirPath + DTKeys.DIRECTORY_REWRITE_ASPX + "/" + requestPage);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 页面请求事件处理
        /// </summary>
        /// <param name="sender">事件的源</param>
        /// <param name="e">包含事件数据的 EventArgs</param>
        private void ReUrl_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context       = ((HttpApplication)sender).Context;
            string      requestDomain = context.Request.Url.Authority.ToLower(); //获得当前域名(含端口号)
            string      requestPath   = context.Request.Path.ToLower();          //获得当前页面(含目录)

            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();     //获得站点配置信息

            //如果虚拟目录(不含安装目录)与站点根目录名相同则不需要重写
            if (IsDirExist(DTKeys.CACHE_SITE_DIRECTORY, siteConfig.webpath, siteConfig.webpath, requestPath))
            {
                return;
            }

            //检查网站重写状态0表示不开启重写、1开启重写、2生成静态
            if (siteConfig.staticstatus == 0)
            {
                #region 站点不开启重写处理方法=======================================
                //遍历URL字典,匹配URL页面部分
                foreach (Model.url_rewrite model in SiteUrls.GetSiteUrls().Urls)
                {
                    //查找到与页面部分匹配的节点
                    if (model.page == requestPath.Substring(requestPath.LastIndexOf("/") + 1))
                    {
                        //如果该页面属于插件页则映射到插件目录,否则映射到频道分类目录
                        if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN)
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}{3}",
                                                              siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, requestPath));
                            return;
                        }
                        else
                        {
                            ////默认的频道目录
                            string defaultPath = SiteDomains.GetSiteDomains().DefaultPath;
                            //获取绑定域名的频道分类目录
                            string domainPath = GetDomainPath(siteConfig.webpath, requestPath, requestDomain);
                            //获取虚拟目录的频道分类目录
                            string categoryPath = GetCategoryPath(siteConfig.webpath, requestPath, requestDomain);
                            if (domainPath != string.Empty)
                            {
                                defaultPath = domainPath;
                            }
                            else if (categoryPath != string.Empty)
                            {
                                defaultPath = categoryPath;
                            }

                            context.RewritePath(string.Format("{0}{1}/{2}{3}",
                                                              siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, defaultPath, requestPath));
                            return;
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 站点开启重写或静态处理方法===================================
                ////默认的频道目录
                string defaultPath = SiteDomains.GetSiteDomains().DefaultPath;
                //获取绑定域名的频道分类目录
                string domainPath = GetDomainPath(siteConfig.webpath, requestPath, requestDomain);
                //获取虚拟目录的频道分类目录
                string categoryPath = GetCategoryPath(siteConfig.webpath, requestPath, requestDomain);

                //遍历URL字典
                foreach (Model.url_rewrite model in SiteUrls.GetSiteUrls().Urls)
                {
                    //如果没有重写表达式则不需要重写
                    if (model.url_rewrite_items.Count == 0 &&
                        Utils.GetUrlExtension(model.page, siteConfig.staticextension) == requestPath.Substring(requestPath.LastIndexOf("/") + 1))
                    {
                        //如果该页面属于插件页则映射到插件目录,否则映射到频道分类目录
                        if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN)
                        {
                            context.RewritePath(string.Format("{0}{1}/{2}/{3}",
                                                              siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, model.page));
                            return;
                        }
                        else
                        {
                            if (domainPath != string.Empty)
                            {
                                defaultPath = domainPath; //如果是绑定的域名访问则将对应的目录赋值
                            }
                            else if (categoryPath != string.Empty)
                            {
                                defaultPath = categoryPath; //如果是虚拟目录访问则将对应的目录赋值
                            }
                            context.RewritePath(string.Format("{0}{1}/{2}/{3}", siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, defaultPath, model.page));
                            return;
                        }
                    }
                    //遍历URL字典的子节点
                    foreach (Model.url_rewrite_item item in model.url_rewrite_items)
                    {
                        if (domainPath != string.Empty)
                        {
                            defaultPath = domainPath; //如果是绑定的域名访问则将对应的目录赋值
                        }
                        else if (categoryPath != string.Empty)
                        {
                            defaultPath = categoryPath; //如果是虚拟目录访问则将对应的目录赋值
                        }
                        string patternPath = siteConfig.webpath;
                        if (domainPath == string.Empty && categoryPath != string.Empty)
                        {
                            patternPath += categoryPath + "/";
                        }
                        string newPattern = Utils.GetUrlExtension(item.pattern, siteConfig.staticextension); //替换扩展名

                        //如果与URL节点匹配则重写
                        if (Regex.IsMatch(requestPath, string.Format("^{0}{1}$", patternPath, newPattern), RegexOptions.None | RegexOptions.IgnoreCase) ||
                            (model.page == "index.aspx" && Regex.IsMatch(requestPath, string.Format("^{0}{1}$", patternPath, item.pattern), RegexOptions.None | RegexOptions.IgnoreCase)))
                        {
                            //如果开启生成静态、不是手机网站且是频道页或首页,则映射重写到HTML目录
                            if (siteConfig.staticstatus == 2 && defaultPath != DTKeys.DIRECTORY_REWRITE_MOBILE &&
                                (model.channel.Length > 0 || model.page.ToLower() == "index.aspx")) //频道页
                            {
                                if (domainPath == string.Empty && categoryPath != string.Empty)
                                {
                                    context.RewritePath(siteConfig.webpath + DTKeys.DIRECTORY_REWRITE_HTML + Utils.GetUrlExtension(requestPath, siteConfig.staticextension, true));
                                }
                                else
                                {
                                    context.RewritePath(siteConfig.webpath + DTKeys.DIRECTORY_REWRITE_HTML + "/" + defaultPath + Utils.GetUrlExtension(requestPath, siteConfig.staticextension, true));
                                }
                                return;
                            }
                            else if (model.type == DTKeys.DIRECTORY_REWRITE_PLUGIN) //插件页
                            {
                                string queryString = Regex.Replace(requestPath, string.Format("{0}{1}", patternPath, newPattern),
                                                                   item.querystring, RegexOptions.None | RegexOptions.IgnoreCase);
                                context.RewritePath(string.Format("{0}{1}/{2}/{3}", siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, DTKeys.DIRECTORY_REWRITE_PLUGIN, model.page),
                                                    string.Empty, queryString);
                                return;
                            }
                            else //其它
                            {
                                string queryString = Regex.Replace(requestPath, string.Format("{0}{1}", patternPath, newPattern),
                                                                   item.querystring, RegexOptions.None | RegexOptions.IgnoreCase);
                                context.RewritePath(string.Format("{0}{1}/{2}/{3}",
                                                                  siteConfig.webpath, DTKeys.DIRECTORY_REWRITE_ASPX, defaultPath, model.page), string.Empty, queryString);
                                return;
                            }
                        }
                    }
                }
                #endregion
            }
        }