private static void ContextBeginRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
var path = context.Request.Path.ToUpperInvariant();
var url = GetUrlWithQueryString(context).ToUpperInvariant();
path = path.Replace(".ASPX.CS", string.Empty);
url = url.Replace(".ASPX.CS", string.Empty);
// to prevent XSS
url = HttpUtility.HtmlEncode(url);
Blog blogInstance = Blog.CurrentInstance;
// bundled scripts and styles are in the ~/scripts and ~/styles
// redirect path from ~/child/scripts/js to ~/scripts/js etc.
if (!blogInstance.IsPrimary)
{
if (url.Contains("/SCRIPTS/") || url.Contains("/STYLES/"))
{
var npath = url.Replace(Blog.CurrentInstance.RelativeWebRoot.ToUpper(), "/");
context.RewritePath(npath);
return;
}
}
if (Utils.IsCurrentRequestForHomepage) {
var front = Page.GetFrontPage();
if (front != null) {
url = front.RelativeLink.ToUpperInvariant();
}
}
var urlContainsFileExtension = url.IndexOf(BlogConfig.FileExtension, StringComparison.OrdinalIgnoreCase) != -1;
if(url.Contains("/FILES/") && url.Contains(".AXDX"))
RewriteFilePath(context, url);
if (url.Contains("/IMAGES/") && url.Contains(".JPGX"))
RewriteImagePath(context, url);
if (urlContainsFileExtension && url.Contains("/POST/")) {
RewritePost(context, url);
}
else if (urlContainsFileExtension && url.Contains("/CATEGORY/")) {
RewriteCategory(context, url);
}
else if (urlContainsFileExtension && url.Contains("/TAG/")) {
RewriteTag(context, url);
}
else if (urlContainsFileExtension && url.Contains("/PAGE/")) {
RewritePage(context, url);
}
else if (urlContainsFileExtension && url.Contains("/CALENDAR/")) {
context.RewritePath(string.Format("{0}default.aspx?calendar=show", Utils.ApplicationRelativeWebRoot), false);
}
else if (urlContainsFileExtension && DefaultPageRequested(context))
{
RewriteDefault(context);
}
else if (urlContainsFileExtension && url.Contains("/AUTHOR/")) {
var author = ExtractTitle(context, url);
context.RewritePath(
string.Format("{0}default{1}?name={2}{3}", Utils.ApplicationRelativeWebRoot, BlogConfig.FileExtension, author, GetQueryString(context)),
false);
}
else if (urlContainsFileExtension && path.Contains("/BLOG.ASPX")) {
context.RewritePath(string.Format("{0}default.aspx?blog=true{1}", Utils.ApplicationRelativeWebRoot, GetQueryString(context)));
}
else {
// If this is blog instance that is in a virtual sub-folder, we will
// need to rewrite the path for URL to a physical file. This includes
// requests such as the homepage (default.aspx), contact.aspx, archive.aspx,
// any of the admin pages, etc, etc.
if (blogInstance.IsSubfolderOfApplicationWebRoot &&
VirtualPathUtility.AppendTrailingSlash(path).IndexOf(blogInstance.RelativeWebRoot, StringComparison.OrdinalIgnoreCase) != -1) {
bool skipRewrite = false;
string rewriteQs = string.Empty;
string rewriteUrl = GetUrlWithQueryString(context);
int qsStart = rewriteUrl.IndexOf("?", StringComparison.Ordinal);
if (qsStart != -1) // remove querystring.
{
rewriteQs = rewriteUrl.Substring(qsStart);
rewriteUrl = rewriteUrl.Substring(0, qsStart);
}
// Want to see if a specific page/file is being requested (something with a . (dot) in it).
// Because Utils.ApplicationRelativeWebRoot may contain a . (dot) in it, pathAfterAppWebRoot
// tells us if the actual path (after the AppWebRoot) contains a dot.
string pathAfterAppWebRoot = rewriteUrl.Substring(Utils.ApplicationRelativeWebRoot.Length);
if (!pathAfterAppWebRoot.Contains(".")) {
if (!rewriteUrl.EndsWith("/"))
rewriteUrl += "/";
rewriteUrl += "default.aspx";
}
else
{
var extension = Path.GetExtension(pathAfterAppWebRoot);
if (extension != null && extension.ToUpperInvariant() == ".AXD")
skipRewrite = true;
}
if (!skipRewrite) {
// remove the subfolder portion. so /subfolder/ becomes /.
rewriteUrl = new Regex(Regex.Escape(blogInstance.RelativeWebRoot), RegexOptions.IgnoreCase).Replace(rewriteUrl, Utils.ApplicationRelativeWebRoot);
context.RewritePath(rewriteUrl + rewriteQs, false);
}
}
}
}