private static bool ProcessCustomRules(CacheController cacheCtrl, string ApplicationPath, Uri Url, PortalAliasInfo objPortalAlias, RewriterAction action)
{
//save and remove the querystring as it gets added back on later
//path parameter specifications will take precedence over querystring parameters
string requestedPath = Url.AbsoluteUri;
string strQueryString = "";
if ((!String.IsNullOrEmpty(Url.Query)))
{
//strQueryString = QueryString.ToString();
strQueryString = Url.Query.Replace("?", "");
requestedPath = requestedPath.Replace(Url.Query, "");
}
string sendTo = "";
//get url rewriting rules
//RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
var rules = cacheCtrl.GetRules().Where(r => r.RuleType == UrlRuleType.Custom);
//iterate through list of rules
UrlRule ruleFound = null;
foreach (UrlRule rule in rules)
{
//check for the existence of the LookFor value
string pattern = "^" + RewriterUtils.ResolveUrl(ApplicationPath, rule.Url) + "$";
Match objMatch = Regex.Match(requestedPath, pattern, RegexOptions.IgnoreCase);
//if there is a match
if (objMatch.Success /* && (string.IsNullOrEmpty(rule.Parameters) || rule.Parameters == strQueryString) */ )
{
//create a new URL using the SendTo regex value
sendTo = RewriterUtils.ResolveUrl(ApplicationPath, Regex.Replace(requestedPath, pattern, rule.RedirectDestination, RegexOptions.IgnoreCase));
string parameters = objMatch.Groups[2].Value;
//process the parameters
//ProcessParameters(objPortalAlias.PortalID, "", -1, ref sendTo, parameters);
ruleFound = rule;
break; //exit as soon as it processes the first match
}
}
//if a match was found to the urlrewrite rules
if (ruleFound != null /*&& string.IsNullOrEmpty(ruleFound.Parameters)*/ )
{
if (!String.IsNullOrEmpty(strQueryString))
{
//add querystring parameters back to SendTo
string[] parameters = strQueryString.Split('&');
string parameterName;
//iterate through the array of parameters
for (int parameterIndex = 0; parameterIndex <= parameters.Length - 1; parameterIndex++)
{
//get parameter name
parameterName = parameters[parameterIndex];
if (parameterName.IndexOf("=") != -1)
{
parameterName = parameterName.Substring(0, parameterName.IndexOf("="));
}
//check if parameter already exists
if (sendTo.IndexOf("?" + parameterName + "=", StringComparison.InvariantCultureIgnoreCase) == -1 &&
sendTo.IndexOf("&" + parameterName + "=", StringComparison.InvariantCultureIgnoreCase) == -1)
{
//add parameter to SendTo value
if (sendTo.IndexOf("?") != -1)
{
sendTo = sendTo + "&" + parameters[parameterIndex];
}
else
{
sendTo = sendTo + "?" + parameters[parameterIndex];
}
}
}
}
if (ruleFound.Action == UrlRuleAction.Rewrite)
{
action.DoReWrite = true;
action.RewriteUrl = sendTo;
action.Raison = "Custom rule";
}
else
{
if (ruleFound.RedirectStatus == 404)
{
action.DoNotFound = true;
action.Raison = "Custom rule";
action.Status = 404;
}
else
{
action.DoRedirect = true;
action.RedirectUrl = sendTo;
action.Raison = "Custom rule";
action.Status = ruleFound.RedirectStatus;
}
}
return true;
}
else
{
return false;
}
}