public override List <UrlRule> GetRules(int PortalId)
        {
            bool UseDisplayName = GetPortalSettingAsBoolean(PortalId, "UseDisplayName");

            List <UrlRule> Rules = new List <UrlRule>();

            UserController uc = new UserController();
            //ArrayList users = uc.GetUsers(PortalId,false, false);

            ArrayList users = UserController.GetUsers(PortalId);

            foreach (UserInfo user in users)
            {
                if (true)
                {
                    var rule = new UrlRule
                    {
                        RuleType   = UrlRuleType.Module,
                        Parameters = "userid=" + user.UserID.ToString(),
                        Action     = UrlRuleAction.Rewrite,
                        Url        = CleanupUrl(UseDisplayName ? user.DisplayName : user.Username)
                    };
#if DNN71
                    if (!string.IsNullOrEmpty(user.VanityUrl))
                    {
                        rule.Url = CleanupUrl(user.VanityUrl);
                    }
#endif

                    Rules.Add(rule);
                }
            }



            var roles = TestableRoleController.Instance.GetRoles(PortalId, r => r.SecurityMode != SecurityMode.SecurityRole);

            foreach (RoleInfo role in roles)
            {
                if (true)
                {
                    var rule = new UrlRule
                    {
                        RuleType   = UrlRuleType.Module,
                        Parameters = "groupid=" + role.RoleID.ToString(),
                        Action     = UrlRuleAction.Rewrite,
                        Url        = CleanupUrl(role.RoleName)
                    };

                    Rules.Add(rule);
                }
            }

            return(Rules);
        }
        public override List<UrlRule> GetRules(int PortalId)
        {
            bool UseDisplayName = GetPortalSettingAsBoolean(PortalId, "UseDisplayName");

            List<UrlRule> Rules = new List<UrlRule>();

            UserController uc = new UserController();
            //ArrayList users = uc.GetUsers(PortalId,false, false);

            ArrayList users = UserController.GetUsers(PortalId);

            foreach (UserInfo user in users)
            {
                if (true)
                {
                    var rule = new UrlRule
                    {
                        RuleType = UrlRuleType.Module,
                        Parameters = "userid=" + user.UserID.ToString(),
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(UseDisplayName ? user.DisplayName : user.Username)
                    };
            #if DNN71
                    if (!string.IsNullOrEmpty(user.VanityUrl)) {
                        rule.Url = CleanupUrl(user.VanityUrl);
                    }
            #endif

                    Rules.Add(rule);
                }
            }

            var roles = TestableRoleController.Instance.GetRoles(PortalId, r => r.SecurityMode != SecurityMode.SecurityRole);

            foreach (RoleInfo role in roles)
            {
                if (true)
                {
                    var rule = new UrlRule
                    {
                        RuleType = UrlRuleType.Module,
                        Parameters = "groupid=" + role.RoleID.ToString(),
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(role.RoleName)
                    };

                    Rules.Add(rule);
                }
            }

            return Rules;
        }
 private static UrlRule getRule(int PortalId, string ctlName)
 {
     var rule = new UrlRule
     {
         TabId = -1,
         RuleType = UrlRuleType.Module,
         Parameters = "ctl="+ctlName,
         RemoveTab = true,
         Action = UrlRuleAction.Rewrite,
         Url = ctlName
     };
     return rule;
 }
        private static UrlRule getRule(int PortalId, string ctlName)
        {
            var rule = new UrlRule
            {
                TabId      = -1,
                RuleType   = UrlRuleType.Module,
                Parameters = "ctl=" + ctlName,
                RemoveTab  = true,
                Action     = UrlRuleAction.Rewrite,
                Url        = ctlName
            };

            return(rule);
        }
 private static UrlRule getRedirect(int PortalId, string ctlName)
 {
     var rule = new UrlRule
     {
         TabId = -1,
         RuleType = UrlRuleType.Module,
         Parameters = "ctl=" + ctlName,
         RemoveTab = true,
         Action = UrlRuleAction.Redirect,
         Url = "ctl/"+ctlName,
         RedirectStatus = 301
         //RedirectDestination = ctlName
     };
     return rule;
 }
        private static UrlRule getRedirect(int PortalId, string ctlName)
        {
            var rule = new UrlRule
            {
                TabId          = -1,
                RuleType       = UrlRuleType.Module,
                Parameters     = "ctl=" + ctlName,
                RemoveTab      = true,
                Action         = UrlRuleAction.Redirect,
                Url            = "ctl/" + ctlName,
                RedirectStatus = 301
                                 //RedirectDestination = ctlName
            };

            return(rule);
        }
        public override List <UrlRule> GetRules(int PortalId)
        {
            List <UrlRule> Rules   = new List <UrlRule>();
            var            Locales = LocaleController.Instance.GetLocales(PortalId).Values;

            foreach (Locale locale in Locales)
            {
                string LocaleUrl;
                // more then 1 locale with same language part
                if (Locales.Count(l => l.Code.Substring(0, 2) == locale.Code.Substring(0, 2)) > 1)
                {
                    LocaleUrl = locale.Code;
                }
                else
                {
                    LocaleUrl = locale.Code.Substring(0, 2);
                }

                var rule = new UrlRule
                {
                    RuleType    = UrlRuleType.Culture,
                    CultureCode = locale.Code,
                    Parameters  = "language=" + locale.Code,
                    Action      = UrlRuleAction.Rewrite,
                    Url         = LocaleUrl.ToLower()
                };
                Rules.Add(rule);
                var ruleRedirect = new UrlRule
                {
                    RuleType            = UrlRuleType.Culture,
                    CultureCode         = locale.Code,
                    Parameters          = "language=" + locale.Code,
                    Action              = UrlRuleAction.Redirect,
                    Url                 = locale.Code.ToLower(),
                    RedirectDestination = rule.Url
                };
                if (rule.Url != ruleRedirect.Url)
                {
                    Rules.Add(ruleRedirect);
                }
            }
            return(Rules);
        }
        private static List <UrlRule> getRules(int PortalId, string ctlName)
        {
            List <UrlRule> Rules = new List <UrlRule>();
            var            rule  = new UrlRule
            {
                TabId      = -1,
                RuleType   = UrlRuleType.Module,
                Parameters = "ctl=" + ctlName,
                RemoveTab  = true,
                Action     = UrlRuleAction.Rewrite,
                Url        = ctlName
            };

            Rules.Add(rule);

            /*
             * rule = new UrlRule
             * {
             *  TabId = -1,
             *  RuleType = UrlRuleType.Module,
             *  Parameters = "ctl=" + ctlName + "&portalid=" + PortalId.ToString(),
             *  RemoveTab = true,
             *  Action = UrlRuleAction.Rewrite,
             *  Url = ctlName
             * };
             * Rules.Add(rule);
             *
             * rule = new UrlRule
             * {
             *  TabId = -1,
             *  RuleType = UrlRuleType.Module,
             *  Parameters = "ctl=" + ctlName,
             *  RemoveTab = true,
             *  Action = UrlRuleAction.Redirect,
             *  Url = "ctl/" + ctlName,
             *  RedirectStatus = 301
             *
             * };
             * Rules.Add(rule);
             */
            return(Rules);
        }
        private static List<UrlRule> getRules(int PortalId, string ctlName)
        {
            List<UrlRule> Rules = new List<UrlRule>();
            var rule = new UrlRule
            {
                TabId = -1,
                RuleType = UrlRuleType.Module,
                Parameters = "ctl=" + ctlName,
                RemoveTab = true,
                Action = UrlRuleAction.Rewrite,
                Url = ctlName
            };
            Rules.Add(rule);
            /*
            rule = new UrlRule
            {
                TabId = -1,
                RuleType = UrlRuleType.Module,
                Parameters = "ctl=" + ctlName + "&portalid=" + PortalId.ToString(),
                RemoveTab = true,
                Action = UrlRuleAction.Rewrite,
                Url = ctlName
            };
            Rules.Add(rule);

            rule = new UrlRule
            {
                TabId = -1,
                RuleType = UrlRuleType.Module,
                Parameters = "ctl=" + ctlName,
                RemoveTab = true,
                Action = UrlRuleAction.Redirect,
                Url = "ctl/" + ctlName,
                RedirectStatus = 301

            };
            Rules.Add(rule);
             */
            return Rules;
        }
        public override List<UrlRule> GetRules(int PortalId)
        {
            List<UrlRule> Rules = new List<UrlRule>();
            var Locales = LocaleController.Instance.GetLocales(PortalId).Values;
            foreach (Locale locale in Locales)
            {
                string LocaleUrl;
                // more then 1 locale with same language part
                if (Locales.Count(l => l.Code.Substring(0, 2) == locale.Code.Substring(0, 2)) > 1)
                    LocaleUrl = locale.Code;
                else
                    LocaleUrl = locale.Code.Substring(0, 2);

                var rule = new UrlRule
                {
                    RuleType = UrlRuleType.Culture,
                    CultureCode = locale.Code,
                    Parameters = "language="+locale.Code,
                    Action = UrlRuleAction.Rewrite,
                    Url = LocaleUrl.ToLower()
                };
                Rules.Add(rule);
                var ruleRedirect = new UrlRule
                {
                    RuleType = UrlRuleType.Culture,
                    CultureCode = locale.Code,
                    Parameters = "language=" + locale.Code,
                    Action = UrlRuleAction.Redirect,
                    Url = locale.Code.ToLower(),
                    RedirectDestination = rule.Url
                };
                if (rule.Url != ruleRedirect.Url)
                {
                    Rules.Add(ruleRedirect);
                }
            }
            return Rules;
        }
        public override List<UrlRule> GetRules(int PortalId)
        {
            List<UrlRule> Rules = new List<UrlRule>();
            Dictionary<string, Locale> dicLocales = LocaleController.Instance.GetLocales(PortalId);
            ProductController pc = new ProductController();
            CategoryController cc = new CategoryController();
            ModuleController mc = new ModuleController();
            ArrayList modules = mc.GetModulesByDefinition(PortalId, "NB_Store_ProductList");
            foreach (ModuleInfo module in modules.OfType<ModuleInfo>().Where(m=> m.IsDeleted == false))
            {
                Hashtable modSettings = mc.GetModuleSettings(module.ModuleID);

                int DetailTabId = Null.NullInteger;
                if (modSettings["lstProductTabs"] != null)
                {
                    string lstProductTabs = modSettings["lstProductTabs"].ToString();
                    int.TryParse(lstProductTabs, out DetailTabId);
                }
                bool RewriteProducts = module.TabID == DetailTabId;

                bool BrowseCategory = Null.NullBoolean;
                string chkBrowseCategory = modSettings["chkBrowseCategory"] == null ? "" :  modSettings["chkBrowseCategory"].ToString();
                Boolean.TryParse(chkBrowseCategory, out BrowseCategory);

                bool IndexProducts = true;
                if (modSettings["chkIndexProducts"] != null)
                {
                    Boolean.TryParse(modSettings["chkIndexProducts"].ToString(), out IndexProducts);
                }

                int CatID = Null.NullInteger;
                string ddlDefaultCategory = modSettings["ddlDefaultCategory"] == null ? "" : modSettings["ddlDefaultCategory"].ToString();
                int.TryParse(ddlDefaultCategory, out CatID);
                foreach (KeyValuePair<string, Locale> key in dicLocales)
                {
                    string CultureCode = key.Value.Code;
                    string RuleCultureCode = (dicLocales.Count > 1 ? CultureCode : null);
                    bool chkCascadeResults = false;
                    bool.TryParse(modSettings["chkCascadeResults"] == null ? "" : modSettings["chkCascadeResults"].ToString(), out chkCascadeResults);

                    if (RewriteProducts)
                    {
                        var prodLst = GetProductList(PortalId, pc, cc, CatID, CultureCode, chkCascadeResults);

                        //var prodLst = pc.GetProductList(PortalId, CatID, CultureCode, false);
                        foreach (ProductListInfo prod in prodLst)
                        {
                            var rule = new UrlRule
                            {
                                RuleType = UrlRuleType.Module,
                                CultureCode = RuleCultureCode,
                                TabId = module.TabID,
                                Parameters = "ProdID=" + prod.ProductID + (CatID == Null.NullInteger ? "" : "&" + "CatID=" + CatID),
                                Action = UrlRuleAction.Rewrite,
                                Url = CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName),
                                InSitemap = IndexProducts
                            };
                            Rules.Add(rule);
                        }
                    }
                    if (BrowseCategory)
                    {
                        var CatRules = GetRulesForCategory(PortalId, CultureCode, module.TabID, CatID, "", pc, cc, RuleCultureCode, chkCascadeResults, RewriteProducts, IndexProducts);
                        Rules.AddRange(CatRules);
                    }
                }
            }
            return Rules;
        }
        private List<UrlRule> GetRulesForCategory(int PortalId, string CultureCode, int TabId, int CatID, string CategoryUrl, ProductController pc, CategoryController cc, string RuleCultureCode, bool chkCascadeResults, bool RewriteProducts, bool IndexProducts)
        {
            List<UrlRule> Rules = new List<UrlRule>();
            var catLst = cc.GetCategories(PortalId, CultureCode, CatID == Null.NullInteger ? 0 : CatID);
            foreach (NB_Store_CategoriesInfo cat in catLst)
            {
                var CatRule = new UrlRule
                {
                    RuleType = UrlRuleType.Module,
                    CultureCode = RuleCultureCode,
                    TabId = TabId,
                    Parameters = "CatID=" + cat.CategoryID,
                    Action = UrlRuleAction.Rewrite,
                    Url = (CategoryUrl == "" ? "" : CategoryUrl + "/") + CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName),
                    InSitemap = IndexProducts
                };
                CatRule.RedirectDestination = CatRule.Parameters.Replace('=', '/').Replace('&', '/') + "/" + CleanupSEO(cat.SEOName == "" ? cat.CategoryName : cat.SEOName);
                CatRule.RedirectDestination = CatRule.RedirectDestination.ToLower();
                if (string.IsNullOrEmpty(CatRule.Url))
                {
                    //continue;
                }
                Rules.Add(CatRule);

                if (RewriteProducts)
                {
                    var productLst = GetProductList(PortalId, pc, cc, cat.CategoryID, CultureCode, chkCascadeResults);
                    //var productLst = pc.GetProductList(PortalId, cat.CategoryID, CultureCode, false);
                    foreach (ProductListInfo prod in productLst)
                    {
                        var rule = new UrlRule
                        {
                            RuleType = UrlRuleType.Module,
                            CultureCode = RuleCultureCode,
                            TabId = TabId,
                            Parameters = "ProdID=" + prod.ProductID + "&" + "CatID=" + cat.CategoryID,
                            Action = UrlRuleAction.Rewrite,
                            Url = (CategoryUrl == "" ? "" : CategoryUrl + "/") + CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName) + "/" + CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName),
                            InSitemap = IndexProducts
                        };
                        Rules.Add(rule);
                    }
                }
                var CatRules = GetRulesForCategory(PortalId, CultureCode, TabId, cat.CategoryID, CatRule.Url, pc, cc, RuleCultureCode, chkCascadeResults, RewriteProducts, IndexProducts);
                Rules.AddRange(CatRules);
            }
            return Rules;
        }
        public List<UrlRule> GetRulesOld(int PortalId)
        {
            List<UrlRule> Rules = new List<UrlRule>();

            Dictionary<string, Locale> dicLocales = LocaleController.Instance.GetLocales(PortalId);
            foreach (KeyValuePair<string, Locale> key in dicLocales)
            {
                string CultureCode = key.Value.Code;
                CategoryController cc = new CategoryController();
                ProductController pc = new ProductController();
                // products alone
                var prodLst = pc.GetProductList(PortalId, Null.NullInteger, CultureCode, false);
                foreach (ProductListInfo prod in prodLst)
                {
                    var rule = new UrlRule
                    {
                        RuleType = UrlRuleType.Module,
                        CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
                        Parameters = "ProdID=" + prod.ProductID ,
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName)
                    };
                    Rules.Add(rule);
                }
                // products and categories
                var catLst = cc.GetCategories(PortalId, CultureCode);
                foreach (NB_Store_CategoriesInfo cat in catLst)
                {
                    var CatRule = new UrlRule
                    {
                        RuleType = UrlRuleType.Module,
                        CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
                        Parameters = "CatID=" + cat.CategoryID,
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName)
                    };
                    CatRule.RedirectDestination = CatRule.Parameters.Replace('=', '/').Replace('&', '/') + "/" + CleanupSEO(cat.SEOName == "" ? cat.CategoryName : cat.SEOName);
                    CatRule.RedirectDestination = CatRule.RedirectDestination.ToLower();
                    Rules.Add(CatRule);

                    var productLst = pc.GetProductList(PortalId, cat.CategoryID, CultureCode, false);
                    foreach (ProductListInfo prod in productLst)
                    {
                        var rule = new UrlRule
                        {
                            RuleType = UrlRuleType.Module,
                            CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
                            Parameters = "ProdID=" + prod.ProductID + "&" + "CatID=" + cat.CategoryID,
                            Action = UrlRuleAction.Rewrite,
                            Url = CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName) +"/"+CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName)
                        };
                        Rules.Add(rule);
                    }
                }
            }
            return Rules;
        }
        // redirection rule from Rewrite rule RedirectDestination
        private UrlRule GetRedirectModuleRule(string CultureCode, int TabId, string Url)
        {
            Url = Url.ToLower();
            UrlRule rule = null;
            bool RemoveTab = TabId == Null.NullInteger;
            var rules = _rules.Where(r => r.RuleType == UrlRuleType.Module && r.RedirectDestination == Url && r.Action == UrlRuleAction.Rewrite /*&& r.RemoveTab == RemoveTab*/);
            //with tabid
            if (TabId != Null.NullInteger)
            {
                var tabRules = rules.Where(r => r.TabId == TabId);
                rule = GetFirstRule(tabRules, CultureCode);
            }
            //without tabid
            //if (rule == null) 22/3/2016
            else
            {
                rule = GetFirstRule(rules, CultureCode);
            }
            if (rule != null)
            {
                rule = new UrlRule()
                {
                    CultureCode = rule.CultureCode,
                    TabId = rule.TabId,
                    RuleType = rule.RuleType,
                    Action = UrlRuleAction.Redirect,
                    Url = rule.RedirectDestination,
                    RedirectDestination = rule.Url,
                    RemoveTab = rule.RemoveTab
                };
            }
            if (rule == null)
            {
                // try to find a rule with de begin of url match a rule RedirectDestination
                rules = _rules.Where(r => r.RuleType == UrlRuleType.Module && Url.StartsWith(r.RedirectDestination+"/")  && r.Action == UrlRuleAction.Rewrite /*&& r.RemoveTab == RemoveTab*/);
                //with tabid
                if (TabId != Null.NullInteger)
                {
                    var tabRules = rules.Where(r => r.TabId == TabId);
                    rule = GetFirstRule(tabRules, CultureCode);
                }
                //without tabid
                //if (rule == null) 22/3/2016
                else
                {
                    rule = GetFirstRule(rules, CultureCode);
                }
                if (rule != null)
                {
                    rule = new UrlRule()
                    {
                        CultureCode = rule.CultureCode,
                        TabId = rule.TabId,
                        RuleType = rule.RuleType,
                        Action = UrlRuleAction.Redirect,
                        Url = rule.RedirectDestination,
                        RedirectDestination = rule.Url,
                        RemoveTab = rule.RemoveTab
                    };
                }

            }
            return rule;
        }
 private UrlRule GetRedirectCustomModuleRule(string CultureCode, int TabId, string Url)
 {
     Url = Url.ToLower();
     UrlRule rule = null;
     bool RemoveTab = TabId == Null.NullInteger;
     var rules = _rules.Where(r => r.RuleType == UrlRuleType.Custom && r.IsMatchRedirectDestination(Url) && r.Action == UrlRuleAction.Rewrite /*&& r.RemoveTab == RemoveTab*/);
     //with tabid
     if (TabId != Null.NullInteger)
     {
         var tabRules = rules.Where(r => r.TabId == TabId);
         rule = GetFirstRule(tabRules, CultureCode);
     }
     //without tabid
     if (rule == null)
     {
         rule = GetFirstRule(rules, CultureCode);
     }
     if (rule != null)
     {
         rule = new UrlRule()
         {
             CultureCode = rule.CultureCode,
             TabId = rule.TabId,
             RuleType = rule.RuleType,
             Action = UrlRuleAction.Redirect,
             Url = rule.RedirectDestination,
             RedirectDestination = rule.Url,
             RemoveTab = rule.RemoveTab
         };
     }
     return rule;
 }
        public static UrlRuleConfiguration GenerateConfig(int portalId)
        {
            //string cacheKey = "UrlRuleConfig" + portalId;

            var config = new UrlRuleConfiguration();
            config.Rules = new List<UrlRule>();

            try
            {
                // 1 cache by portal
                //DnnLog.Trace("Get cache " + portalId );
                //config = (UrlRuleConfiguration)DataCache.GetCache(cacheKey);

                //if ((config == null))
                {
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    Logger.Info("Rebuild cache start " + portalId);

                    //config = new UrlRuleConfiguration { Rules = new List<UrlRule>() };

                    // generate admin page
                    GenerateAdminTab(portalId);
                    //if (false)
                    {

                        var builder = new UrlBuilder();
                        var Rules = builder.BuildUrlMap(portalId);
                        config.Rules.AddRange(Rules);

                        var storedRules = UrlRuleController.GetUrlRules(portalId);
                        foreach (UrlRuleInfo storedRule in storedRules)
                        {
                            if (storedRule.CultureCode == "") storedRule.CultureCode = null;
                            if (storedRule.RedirectDestination == "") storedRule.RedirectDestination = null;
                            if (storedRule.RedirectDestination != null) storedRule.RedirectDestination = storedRule.RedirectDestination.Trim();
                            if (storedRule.Url != null) storedRule.Url = storedRule.Url.Trim();
                        }

                        // add custom rules to cache
                        foreach (UrlRuleInfo storedRule in storedRules.Where(r => r.RuleType == (int)UrlRuleType.Custom /* && r.RuleAction == (int)UrlRuleAction.Redirect */)) // custom rule
                        {
                            UrlRule rule = new UrlRule()
                            {
                                RuleType = (UrlRuleType)storedRule.RuleType,
                                CultureCode = storedRule.CultureCode,
                                TabId = storedRule.TabId,
                                Parameters = storedRule.Parameters,
                                Action = (UrlRuleAction)storedRule.RuleAction,
                                RemoveTab = storedRule.RemoveTab,
                                RedirectDestination = storedRule.RedirectDestination,
                                RedirectStatus = storedRule.RedirectStatus,
                                Url = storedRule.Url
                            };
                            config.Rules.Add(rule);
                        }

                        // add rule for sitemap.xml rewrite to sitemap.aspx
                        /* replaced by a handler
                        UrlRule sitemapRule = new UrlRule()
                        {
                            RuleType = UrlRuleType.Custom,
                            Action = UrlRuleAction.Rewrite,
                            RedirectDestination = "~/Sitemap.aspx",
                            Url = ".*sitemap.xml"
                        };
                        config.Rules.Add(sitemapRule);
                        */
                        foreach (UrlRule rule in config.Rules)
                        {
                            if (rule.CultureCode == "") rule.CultureCode = null;
                            if (rule.RedirectDestination == "") rule.RedirectDestination = null;
                            rule.Url = rule.Url.ToLower();
                            // set RedirectDestination for automatic redirections corresponding to rewrite rule
                            if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Rewrite && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                rule.RedirectDestination = rule.Parameters.Replace('=', '/').Replace('&', '/');
                            }
                            else if (rule.RuleType == UrlRuleType.Module && rule.Action == UrlRuleAction.Redirect && string.IsNullOrEmpty(rule.RedirectDestination))
                            {
                                var rewriteRule = config.Rules.FirstOrDefault(r => r.RuleType == UrlRuleType.Module && r.Parameters == rule.Parameters && r.Action == UrlRuleAction.Rewrite);
                                if (rewriteRule != null)
                                {
                                    rule.RedirectDestination = rewriteRule.Url;
                                }
                            }
                            if (rule.RedirectDestination != null)
                            {
                                rule.RedirectDestination = rule.RedirectDestination.ToLower();
                            }
                        }

                        var OldRules = new List<UrlRuleInfo>(storedRules);

                        var DistuctRules = config.Rules.Where(r => r.RuleType != UrlRuleType.Custom)
                                                        .GroupBy(r => new { r.RuleType, r.CultureCode, r.TabId, r.Url })
                                                        .Select(g => g.First());

                        foreach (UrlRule rule in DistuctRules)
                        {
                            var ruleInfoLst = storedRules.Where(r => r.RuleType == (int)rule.RuleType &&
                                                                                r.CultureCode == rule.CultureCode &&
                                                                                r.TabId == rule.TabId &&
                                                                                r.Url == rule.Url);

                            UrlRuleInfo ruleInfo = ruleInfoLst.FirstOrDefault();
                            if (ruleInfo == null)
                            {
                                ruleInfo = new UrlRuleInfo()
                                {
                                    PortalId = portalId,
                                    DateTime = DateTime.Now,
                                    RuleType = (int)rule.RuleType,
                                    CultureCode = rule.CultureCode,
                                    TabId = rule.TabId,
                                    Url = rule.Url,

                                    Parameters = rule.Parameters,
                                    RuleAction = (int)rule.Action,
                                    RemoveTab = rule.RemoveTab,
                                    RedirectDestination = rule.RedirectDestination,
                                    RedirectStatus = rule.RedirectStatus
                                };
                                ruleInfo.UrlRuleId = UrlRuleController.AddUrlRule(ruleInfo);
                                Logger.Info("AddUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                storedRules.Add(ruleInfo);
                            }
                            else
                            {
                                bool FirstOne = true; // for auto correction of double stored rules
                                foreach (var r in ruleInfoLst)
                                {
                                    OldRules.Remove(r);
                                    if (FirstOne)
                                    {
                                        if (ruleInfo.Parameters != rule.Parameters ||
                                            ruleInfo.RuleAction != (int)rule.Action ||
                                            ruleInfo.RemoveTab != rule.RemoveTab ||
                                            ruleInfo.RedirectDestination != rule.RedirectDestination ||
                                            ruleInfo.RedirectStatus != rule.RedirectStatus)
                                        {
                                            Logger.Info("UpdateUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId +

                                                (ruleInfo.Parameters == rule.Parameters) + "/" +
                                                (ruleInfo.RuleAction == (int)rule.Action) + "/" +
                                                (ruleInfo.RemoveTab == rule.RemoveTab) + "/" +
                                                (ruleInfo.RedirectDestination == rule.RedirectDestination) + "/" +
                                                (ruleInfo.RedirectStatus == rule.RedirectStatus) + "-" +

                                                ruleInfo.Parameters + "/" + rule.Parameters + "/" +
                                                ruleInfo.RuleAction + "/" + (int)rule.Action + "/" +
                                                ruleInfo.RemoveTab + "/" + rule.RemoveTab + "/" +
                                                ruleInfo.RedirectDestination + "/" + rule.RedirectDestination + "/" +
                                                ruleInfo.RedirectStatus + "/" + rule.RedirectStatus +

                                                ")");

                                            ruleInfo.Parameters = rule.Parameters;
                                            ruleInfo.RuleAction = (int)rule.Action;
                                            ruleInfo.RemoveTab = rule.RemoveTab;
                                            ruleInfo.RedirectDestination = rule.RedirectDestination;
                                            ruleInfo.RedirectStatus = rule.RedirectStatus;
                                            UrlRuleController.UpdateUrlRule(ruleInfo);

                                        }
                                    }
                                    else
                                    {
                                        UrlRuleController.DeleteUrlRule(ruleInfo.UrlRuleId);
                                        Logger.Info("DeleteUrlRule (UrlRuleId=" + ruleInfo.UrlRuleId + ")");
                                    }

                                    FirstOne = false;
                                }
                            }
                        }

                        foreach (var storedRule in OldRules.Where(r => r.RuleType == (int)UrlRuleType.Tab ||
                                                                        r.RuleType == (int)UrlRuleType.Module))
                        {
                            if (storedRule.RuleAction == (int)UrlRuleAction.Rewrite)
                            {
                                var actualRule = Rules.FirstOrDefault(r => r.RuleType == (UrlRuleType)storedRule.RuleType &&
                                                                            r.CultureCode == storedRule.CultureCode &&
                                                                            r.TabId == storedRule.TabId &&
                                                                            r.Parameters == storedRule.Parameters);

                                if (actualRule != null)
                                {
                                    UrlRule rule = new UrlRule()
                                    {
                                        RuleType = (UrlRuleType)storedRule.RuleType,
                                        CultureCode = storedRule.CultureCode,
                                        TabId = storedRule.TabId,
                                        Parameters = storedRule.Parameters,
                                        Action = UrlRuleAction.Redirect,
                                        RemoveTab = storedRule.RemoveTab,
                                        RedirectDestination = actualRule.Url,
                                        RedirectStatus = storedRule.RedirectStatus,
                                        Url = storedRule.Url
                                    };
                                    config.Rules.Add(rule);
                                }
                            }
                        }
                    }
                    //DnnLog.MethodExit();
                    //DataCache.SetCache("UrlRuleConfig", config, TimeSpan.FromDays(1));
                    //int intCacheTimeout = 20 * Convert.ToInt32(DotNetNuke.Entities.Host.Host.PerformanceSetting);

                    timer.Stop();
                    double responseTime = timer.ElapsedMilliseconds / 1000.0;

                    Logger.Info("Rebuild cache " + portalId + " (" + responseTime + ")");
                    //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(intCacheTimeout));

                    //var onRemove = new CacheItemRemovedCallback(config.RemovedCallBack);

                    //DateTime absoluteExpiration = DateTime.UtcNow.Add(TimeSpan.FromMinutes(intCacheTimeout));

                    //DataCache.SetCache(cacheKey, config, null, absoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, onRemove, false);

                }
            }
            catch (Exception ex)
            {
                //log it
                var objEventLog = new EventLogController();
                var objEventLogInfo = new LogInfo() { LogTypeKey = "GENERAL_EXCEPTION" };
                objEventLogInfo.AddProperty("UrlRewriter.UrlRuleConfig", "GetConfig Failed");
                objEventLogInfo.AddProperty("ExceptionMessage", ex.Message);
                objEventLogInfo.AddProperty("PortalId", portalId.ToString());
                //objEventLogInfo.LogTypeKey = EventLogController.EventLogType.HOST_ALERT.ToString();

                objEventLogInfo.AddProperty("Exception Type", ex.GetType().ToString());
                objEventLogInfo.AddProperty("Message", ex.Message);
                objEventLogInfo.AddProperty("Stack Trace", ex.StackTrace);
                if (ex.InnerException != null)
                {
                    objEventLogInfo.AddProperty("Inner Exception Message", ex.InnerException.Message);
                    objEventLogInfo.AddProperty("Inner Exception Stacktrace", ex.InnerException.StackTrace);
                }
                objEventLogInfo.BypassBuffering = true;

                objEventLog.AddLog(objEventLogInfo);
                Logger.Error(ex);
                //DataCache.SetCache(cacheKey, config, TimeSpan.FromMinutes(60));
            }

            return config;
        }
        public override List <UrlRule> GetRules(int PortalId)
        {
            bool useKeyWords    = GetPortalSettingAsBoolean(PortalId, "UseKeyWords");
            bool RemoveHomePage = GetPortalSettingAsBoolean(PortalId, "RemoveHomePage");


            List <UrlRule> Rules                   = new List <UrlRule>();
            TabController  tc                      = new TabController();
            Locale         DefaultLocale           = LocaleController.Instance.GetDefaultLocale(PortalId);
            Dictionary <string, Locale> dicLocales = LocaleController.Instance.GetLocales(PortalId);
            PortalInfo objPortal                   = new PortalController().GetPortal(PortalId, DefaultLocale.Code);
            int        DefaultHomeTabId            = -1;

            if (objPortal != null)
            {
                DefaultHomeTabId = objPortal.HomeTabId;
            }


            var tabs = tc.GetTabsByPortal(PortalId).Values;

            foreach (TabInfo tab in tabs)
            {
                if (/*tab.PortalID > -1 && !tab.TabPath.StartsWith(@"//Admin//") && tab.TabPath != @"//Admin" &&*/ !tab.DisableLink /*&& tab.TabType == TabType.Normal*/ && !tab.IsDeleted)
                {
                    bool RemoveTab        = RemoveHomePage && tab.TabID == DefaultHomeTabId;
                    bool MLNeutralHomeTab = LocaleController.Instance.GetLocales(PortalId).Count > 1 &&
                                            tab.TabID == DefaultHomeTabId && string.IsNullOrEmpty(tab.CultureCode);

                    string cultureCode     = tab.CultureCode;
                    string ruleCultureCode = (dicLocales.Count > 1 ? cultureCode : null);

                    var rule = new UrlRule
                    {
                        RuleType    = UrlRuleType.Tab,
                        CultureCode = ruleCultureCode,
                        TabId       = tab.TabID,
                        Parameters  = "tabid=" + tab.TabID.ToString(),
                        Action      = UrlRuleAction.Rewrite,
                        Url         = CleanupUrl(GetTabUrl(tab, useKeyWords)),
                        RemoveTab   = RemoveTab && !MLNeutralHomeTab
                    };

                    TabInfo parentTab = tab;
                    while (parentTab.ParentId != Null.NullInteger)
                    {
                        parentTab = tc.GetTab(parentTab.ParentId, PortalId, false);
                        rule.Url  = CleanupUrl(GetTabUrl(parentTab, useKeyWords)) + "/" + rule.Url;
                    }
#if DNN71
                    var tabUrl = tab.TabUrls.SingleOrDefault(t => /*t.IsSystem
                                                                   * &&*/t.HttpStatus == "200" &&
                                                             t.SeqNum == 0);
                    if (tabUrl != null && tabUrl.Url.Trim('/') != "")
                    {
                        rule.Url = tabUrl.Url.Trim('/');
                    }
#endif
                    bool ok2Continue = true;

                    if (tab.TabType != TabType.Normal)
                    {
                        string redirUrl = "";
                        switch (tab.TabType)
                        {
                        case TabType.Tab:
                            //Get the tab being linked to
                            TabInfo tempTab = new TabController().GetTab(Int32.Parse(tab.Url), tab.PortalID, false);
                            if (tempTab == null)
                            {
                                Logger.Error(string.Format("Tab {0} of portal {1} redirects to a tab ({2}) that doesn't exist anymore", tab.TabPath, tab.PortalID, tab.Url));
                                ok2Continue = false;
                            }
                            else
                            {
                                redirUrl = tempTab.TabPath.Replace("//", "/").Substring(1);
                            }
                            break;

                        case TabType.File:
                            //var file = FileManager.Instance.GetFile(Int32.Parse(tab.Url.Substring(7)));
                            //tabUrl = file.RelativePath;
                            break;

                        case TabType.Url:
                            redirUrl = tab.Url;
                            break;
                        }
                        rule.Action              = UrlRuleAction.Redirect;
                        rule.RedirectStatus      = tab.PermanentRedirect ? 301 : 302;
                        rule.RedirectDestination = redirUrl;
                    }

                    if (ok2Continue)
                    {
                        Rules.Add(rule);
                        var ruleRedirect = new UrlRule
                        {
                            RuleType            = UrlRuleType.Tab,
                            CultureCode         = tab.CultureCode,
                            TabId               = tab.TabID,
                            Parameters          = "tabid=" + tab.TabID.ToString(),
                            Action              = UrlRuleAction.Redirect,
                            Url                 = tab.TabPath.Replace("//", "/").TrimStart('/').ToLower(),
                            RedirectDestination = rule.Url,
                            RemoveTab           = RemoveTab && !MLNeutralHomeTab
                        };
                        if (rule.Url != ruleRedirect.Url)
                        {
                            Rules.Add(ruleRedirect);
                        }

                        // if RemoveTab for multi-language and neutral home page
                        // add a culture specific rewrite
                        if (RemoveTab && MLNeutralHomeTab)
                        {
                            var ruleNeutral = new UrlRule
                            {
                                RuleType    = UrlRuleType.Tab,
                                CultureCode = DefaultLocale.Code,
                                TabId       = tab.TabID,
                                Parameters  = rule.Parameters,
                                Action      = UrlRuleAction.Rewrite,
                                Url         = rule.Url,
                                RemoveTab   = true
                            };
                            Rules.Add(ruleNeutral);
                            var ruleRedirectNeutral = new UrlRule
                            {
                                RuleType            = UrlRuleType.Tab,
                                CultureCode         = DefaultLocale.Code,
                                TabId               = tab.TabID,
                                Parameters          = ruleRedirect.Parameters,
                                Action              = UrlRuleAction.Redirect,
                                Url                 = ruleRedirect.Url,
                                RedirectDestination = ruleNeutral.Url,
                                RemoveTab           = true
                            };
                            if (ruleNeutral.Url != ruleRedirectNeutral.Url)
                            {
                                Rules.Add(ruleRedirectNeutral);
                            }
                        }
                    }
                }
            }
            return(Rules);
        }
        public override List<UrlRule> GetRules(int PortalId)
        {
            bool useKeyWords = GetPortalSettingAsBoolean(PortalId, "UseKeyWords");
            bool RemoveHomePage = GetPortalSettingAsBoolean(PortalId, "RemoveHomePage");

            List<UrlRule> Rules = new List<UrlRule>();
            TabController tc = new TabController();
            Locale DefaultLocale = LocaleController.Instance.GetDefaultLocale(PortalId);
            PortalInfo objPortal = new PortalController().GetPortal(PortalId, DefaultLocale.Code);
            int DefaultHomeTabId = -1;
            if (objPortal != null)
                DefaultHomeTabId = objPortal.HomeTabId;

            var tabs = tc.GetTabsByPortal(PortalId).Values;
            foreach (TabInfo tab in tabs)
            {
                if ( /*tab.PortalID > -1 && !tab.TabPath.StartsWith(@"//Admin//") && tab.TabPath != @"//Admin" &&*/  !tab.DisableLink /*&& tab.TabType == TabType.Normal*/ && !tab.IsDeleted)
                {

                    bool RemoveTab = RemoveHomePage && tab.TabID == DefaultHomeTabId;
                    bool MLNeutralHomeTab = LocaleController.Instance.GetLocales(PortalId).Count > 1 &&
                            tab.TabID == DefaultHomeTabId && string.IsNullOrEmpty(tab.CultureCode);

                    var rule = new UrlRule
                    {
                        RuleType = UrlRuleType.Tab,
                        CultureCode = tab.CultureCode,
                        TabId = tab.TabID,
                        Parameters = "tabid=" + tab.TabID.ToString(),
                        Action = UrlRuleAction.Rewrite,
                        Url = CleanupUrl(GetTabUrl(tab, useKeyWords)),
                        RemoveTab = RemoveTab && !MLNeutralHomeTab
                    };

                    TabInfo parentTab = tab;
                    while (parentTab.ParentId != Null.NullInteger)
                    {
                        parentTab = tc.GetTab(parentTab.ParentId, PortalId, false);
                        rule.Url = CleanupUrl(GetTabUrl(parentTab, useKeyWords)) + "/" + rule.Url;
                    }
            #if DNN71
                    var tabUrl = tab.TabUrls.SingleOrDefault(t =>  /*t.IsSystem
                                                                &&*/ t.HttpStatus == "200"
                                                                        && t.SeqNum == 0);
                    if (tabUrl != null && tabUrl.Url.Trim('/') != "")
                    {
                        rule.Url = tabUrl.Url.Trim('/');
                    }
            #endif

                    if (tab.TabType != TabType.Normal)
                    {
                        string redirUrl = "";
                        switch (tab.TabType)
                        {
                            case TabType.Tab:
                                //Get the tab being linked to
                                TabInfo tempTab = new TabController().GetTab(Int32.Parse(tab.Url), tab.PortalID, false);
                                redirUrl = tempTab.TabPath.Replace("//", "/").Substring(1);
                                break;
                            case TabType.File:
                                //var file = FileManager.Instance.GetFile(Int32.Parse(tab.Url.Substring(7)));
                                //tabUrl = file.RelativePath;
                                break;
                            case TabType.Url:
                                redirUrl = tab.Url;
                                break;
                        }
                        rule.Action = UrlRuleAction.Redirect;
                        rule.RedirectStatus = tab.PermanentRedirect ? 301 : 302;
                        rule.RedirectDestination = redirUrl;
                    }

                    Rules.Add(rule);
                    var ruleRedirect = new UrlRule
                    {
                        RuleType = UrlRuleType.Tab,
                        CultureCode = tab.CultureCode,
                        TabId = tab.TabID,
                        Parameters = "tabid=" + tab.TabID.ToString(),
                        Action = UrlRuleAction.Redirect,
                        Url = tab.TabPath.Replace("//", "/").TrimStart('/').ToLower(),
                        RedirectDestination = rule.Url,
                        RemoveTab = RemoveTab && !MLNeutralHomeTab
                    };
                    if (rule.Url != ruleRedirect.Url)
                    {
                        Rules.Add(ruleRedirect);
                    }

                    // if RemoveTab for multi-language and neutral home page
                    // add a culture specific rewrite
                    if (RemoveTab && MLNeutralHomeTab)
                    {
                        var ruleNeutral = new UrlRule
                        {
                            RuleType = UrlRuleType.Tab,
                            CultureCode = DefaultLocale.Code,
                            TabId = tab.TabID,
                            Parameters = rule.Parameters,
                            Action = UrlRuleAction.Rewrite,
                            Url = rule.Url,
                            RemoveTab = true
                        };
                        Rules.Add(ruleNeutral);
                        var ruleRedirectNeutral = new UrlRule
                        {
                            RuleType = UrlRuleType.Tab,
                            CultureCode = DefaultLocale.Code,
                            TabId = tab.TabID,
                            Parameters = ruleRedirect.Parameters,
                            Action = UrlRuleAction.Redirect,
                            Url = ruleRedirect.Url,
                            RedirectDestination = ruleNeutral.Url,
                            RemoveTab = true
                        };
                        if (ruleNeutral.Url != ruleRedirectNeutral.Url)
                        {
                            Rules.Add(ruleRedirectNeutral);
                        }

                    }
                }
            }
            return Rules;
        }