Problem with .htaccess RewriteRule

Mar 29, 2010 at 9:37 PM

So I have a site that I loaded onto. This url is part of several that share the same web folder, I use an .htaccess file to control the url rewriting. I can get the home page of the blog to display correctly, but if I try to login it errors out because it's trying to go here << note the /nwcs/ that is the name of the folder that the files are in. So in effect when the site loads it;s going to, but when you click on the login button it rewrites it to so the application does not process and it errors out.

How do I get around this?


Mar 30, 2010 at 6:39 AM

I think you're having the same type of problem a lot of GoDaddy customers have when they are have their blog in a subfolder, and GoDaddy's URL rewriting tool rewrites URLs to the subfolder (discussed here).

ASP.NET sees the URL that is rewritten by .htaccess, instead of the original URL.  Here's an idea I have, which might work (untested).

1.  Create a file named FolderRewrite.cs in the App_Code folder.  In it, put this code in there (it's an HTTP module):

using System;
using System.Collections.Generic;
using System.Web;

public class FolderRewrite : IHttpModule
    public void Dispose()
        // Nothing to dispose

    public void Init(HttpApplication context)
        context.BeginRequest += new EventHandler(context_BeginRequest);

    private void context_BeginRequest(object sender, EventArgs e)
        HttpContext context = ((HttpApplication)sender).Context;
        if (context == null) { return; }

        string beginningText = "/nwcs";
        if (context.Request.Url.PathAndQuery.StartsWith(beginningText, StringComparison.OrdinalIgnoreCase))
            context.RewritePath(context.Request.Url.PathAndQuery.Remove(0, beginningText.Length), true);

2.  In the web.config file, register this HTTP module in the <httpModules> section and the <modules> section.  The latter is just for IIS7.  Make sure this new item is FIRST -- before the other items.

	<add name="FolderRewrite" type="FolderRewrite"/>
	..... existing items ......

	<add name="FolderRewrite" type="FolderRewrite"/>
	..... existing items ......


Then see what happens.

Mar 30, 2010 at 9:27 PM

Now I get an error about how "/default.aspx" maps to another application, which is not allowed:

Server Error in '/nwcs' Application.

The virtual path '/login.aspx' maps to another application, which is not allowed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The virtual path '/login.aspx' maps to another application, which is not allowed.

Source Error: 

Line 23:         if (context.Request.Url.PathAndQuery.StartsWith(beginningText, StringComparison.OrdinalIgnoreCase))
Line 24:         {
Line 25:             context.RewritePath(context.Request.Url.PathAndQuery.Remove(0, beginningText.Length), true);
Line 26:         }
Line 27:     }

Source File: c:\sites\single45\mhestnes1\webroot\nwcs\app_code\folderrewrite.cs    Line: 25 

Mar 30, 2010 at 10:34 PM

I don't think the HTTP module is going to work as I'd thought it might.  I would remove the module (the .CS file) and remove the entries you added into the web.config file.

There's a couple of other possibilities.  Here's something that I partially tested and might work.  In the web.config file, there's an appSetting for "BlogEngine.VirtualPath".  It's in the <appSettings> section.  Change the one in your web.config file so it looks like this:

<add key="BlogEngine.VirtualPath" value="~/../"/>
The original one you have has a value of "~/".  That's been changed above to "~/../".

Mar 30, 2010 at 11:01 PM

Hi Ben,

Thanks for the reply. I tried changing the "BlogEngine.VirtualPath" and now I get this error when I try to navigate to


Server Error in '/nwcs' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /nwcs/nwcs/error404.aspx

Mar 31, 2010 at 12:12 AM

I'm not sure exactly where the initial problem might be occurring, but I can see that /nwcs is in the "requested URL" twice.  I would add a condition to your rewrite rule so it doesn't do a rewrite if /nwcs is already in the URL.  This may not solve this particular problem, but could help, and is probably a good idea to have in there -- just in case.  The condition you would add would (probably) look like:

RewriteCond %{REQUEST_URI} !nwcs/ [NC]

Mar 31, 2010 at 12:28 AM

Thanks Ben, That did the trick. Your a freakin' GENIUS!!!

I was almost bald with all the hair I pulled out trying to get this to work.


Mar 31, 2010 at 12:39 AM

It's good you got past the error.  But I notice when I go to the site, all the links have /nwcs in them.  I'm guessing that's not what you want.

Are you still using the revised "BlogEngine.VirtualPath" appSetting?

With the Standard theme, the typical links are constructed with Utils.AbsoluteWebRoot, which uses RelativeWebRoot ... and RelativeWebRoot uses that appSetting.  If the appSetting value is at "~/../", then the links should not be including /nwcs in them.  So I'm not sure right now why /nwcs is in the links, assuming you left the modified BlogEngine.VirtualPath appSetting we put in there earlier.

Mar 31, 2010 at 12:43 AM

When I add that value to my web.config then the site doesn't work any more :(



Mar 31, 2010 at 12:46 AM

Well, if you're getting unhandled errors that are redirecting you to the generic error404.aspx page (when using the modified appSetting, or even anytime), I would disable custom errors in the web.config file.  If you change the <customErrors> tag to the following, you should get some error details that we can look at -- rather than the generic redirect to error404.aspx.

<customErrors mode="Off">