Apache Sytle URL Rewriting (mod_rewrite) For BlogEngine.NET

Topics: ASP.NET 2.0, Business Logic Layer
Nov 4, 2007 at 3:24 PM
Edited Nov 4, 2007 at 3:25 PM
Hi BlogEngine.net Team,

I just started using blogengine.net this weekend, and I have to say good job. I like the feel of the code because it is very slim but powerful at the same time. So it was very easy to customize for my own needs. I am e-mailing you because I have found an opportunity for myself to help out with blogengine.net, if given the chance.

I took the initiative and wrote my own fully managed URL rewriter that followed the Apache modrewrite syntax to the "t". The best thing about my solution over others such as ISAPIRewrite is that you don't need access to IIS in order for it to work, because it just works through the built in IHttpModule interface. The other benefit is it has a familiar interface for the majority of the converts that are use to Apache mod_rewrite. I have included a sample of the config file below:

--------------------------------------------------------
RewriteEngine On

# redirect to canonical domain
RewriteCond %{HTTP_HOST} !^coderjournal\.com NC
RewriteRule (.*) http://coderjournal.com/$1 R=301,L

RewriteRule ^/blog.aspx /default.aspx?blog=true NC,L
RewriteRule ^/post/(0-9{4})/(0-9{2})/(a-z0-9\-+).aspx /post.aspx?year=$1&month=$2&slug=$3 NC,L
RewriteRule ^/category/(a-z0-9\-+).aspx /category.aspx?slug=$1 NC,L
RewriteRule ^/page/(a-z0-9\-+).aspx /page.aspx?slug=$1 NC,L
RewriteRule ^/author/(a-z0-9\-+).aspx /author.aspx?name=$1 NC,L
--------------------------------------------------------
I have uploaded the work I have done to http://coderjournal.com/BlogEngine.Web.zip, if anybody wants to download and check it out.

There are two ways to add rules to my engine, either compiled in the application via the global.asax or via a rules file as illustrated in rewriter.rules. What I did to change your code was remove the WwwSubDomainModule and UrlRewrite Module and replace it all with my simple rules as illustrated in the Global.asax file. Please look over my code and tell me what you think. I would really like to join your team and help out in the URL Rewriting and other aspects of BlogEngine.Net.

If you would like more information on my URL Rewriter or if you would like to contact me. My e-mail and IM is nberardi@gmail.com.

I also think if you like what I have done here we can offer very customizable URLs much like what WordPress offers to their users, because the architecture is very expendable meaning with no work at all I can define user url rewriting rules like the ones you see above and application URL rewriting rules like the ones that you use to change /archive/year/month/day/name.aspx to the actual handler that does all the work.

I would like to discuss this with you some more if you will give me the time.

Thanks,
Nick
Nov 17, 2007 at 5:56 AM
Nick your code here.... Will it work on windows 2003 server IIS 6, to a sql 2000 database?
I have tried to get BlogEngine working on a windows 2003 server with sql 2000. My problem is that everything going to the database.
1. header link of blog ect. (anything going to /post/2007/ect. ect. doesn't work. Can I just drop blogengine.web.zip into my site
http://mywebsite.com/dave (dave being a virtual folder in IIS). Do I need tochange anything in the web.config? Or anything else other than the sql.config to my database?.

Thanks in advance.
Nov 17, 2007 at 10:34 PM
So, why would BE.NET need this, since it already has the rewrite rules you specify written in, with no "ISAPIRewrite" or "access to IIS" required?

However, if you can rewrite pages that don't end in .aspx, on IIS 6, then that would be impressive. But since you're using the same IHttpModule thing as every other ASP.NET URL rewriting thing in the world, I kind of doubt that it works. (Besides, all your examples end in .aspx)
Nov 17, 2007 at 11:18 PM
Hi Domenic
I am just asking the question. It seems like there many of us out there trying to get BE.NET working on windows 2003 server with sql 2000.
I thought this may be the possible problem. However I dont know that for sure.
Many of us are having the same problem as to not getting to the post and getting 404's.

Example if I post something for example:
http://mywebsite.org/dave website.

My post will right to the database fine, and the post will display on the site fine.
However the title of the blog link that was posted goes to http://mywebsite.org/dave/post/test-blog.aspx
Which returns a 404 error when I click on the link because there isn't a /dave/post folder on the website.
I am just trying to figure out why its not working. I don't have this behavior in xp machine, so I am assuming it has something to do with how the app works under Windows 2003 server.

Any light you can shed on this would be greatly appreciated.
Nov 18, 2007 at 1:13 AM
Oh interesting. Yeah, the folders not existing problem used to be a problem with URL rewriting in IIS; now the problem I think is the lack of aspx extension. (IIS 7 will finally fix both.) My host's IIS6/Windows 2003 setup works fine for nonexistant folders, though.

Maybe check your web.config?
Nov 18, 2007 at 9:58 AM
Hi Domenic.

I think this is pretty much all I changed in the web config.

Original:

<BlogEngine>
<blogProvider defaultProvider="XmlBlogProvider">
<providers>
<add name="XmlBlogProvider" type="BlogEngine.Core.Providers.XmlBlogProvider"/>
<add name="MSSQLBlogProvider" type="BlogEngine.Core.Providers.MSSQLBlogProvider"/>
</providers>
</blogProvider>
</BlogEngine>



Changed to:

<BlogEngine>
<blogProvider defaultProvider="MSSQLBlogProvider">
<providers>

<add name="MSSQLBlogProvider" type="BlogEngine.Core.Providers.MSSQLBlogProvider"/>
</providers>
</blogProvider>
</BlogEngine>




--------------

Original:
<httpModules>
<add name="UrlRewrite" type="BlogEngine.Core.Web.HttpModules.UrlRewrite"/>
<add name="CompressionModule" type="BlogEngine.Core.Web.HttpModules.CompressionModule"/>
<add name="ReferrerModule" type="BlogEngine.Core.Web.HttpModules.ReferrerModule"/>
<add name="WwwSubDomainModule" type="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule"/>
<!--Remove the default ASP.NET modules we don't need -->
<remove name="PassportAuthentication" />
<remove name="Profile" />
<remove name="AnonymousIdentification" />
</httpModules>

Changed to:
<httpModules>
<add name="UrlRewrite" type="BlogEngine.Core.Web.HttpModules.UrlRewrite"/>
<add name="CompressionModule" type="BlogEngine.Core.Web.HttpModules.CompressionModule"/>
<add name="ReferrerModule" type="BlogEngine.Core.Web.HttpModules.ReferrerModule"/>
<add name="WwwSubDomainModule" type="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule"/>
<!--Remove the default ASP.NET modules we don't need -->

Commented out these three lines
<!-- <remove name="PassportAuthentication" />
<remove name="Profile" />
<remove name="AnonymousIdentification" /> -->
</httpModules>


I also had to change the sql script to work with SQl 2000

anywhere there was:
WITH (IGNOREDUPKEY = OFF) ON PRIMARY
I removed

Original Example:

CREATE TABLE dbo.be_Categories(
CategoryIDuniqueidentifier ROWGUIDCOL NOT NULL CONSTRAINT DF_be_Categories_CategoryID DEFAULT (newid()),
CategoryNamevarchar(50) COLLATE SQLLatin1GeneralCP1CI_AS NULL,
CONSTRAINT PK_be_Categories PRIMARY KEY CLUSTERED
(
CategoryID ASC
)WITH (IGNOREDUPKEY = OFF) ON PRIMARY
) ON PRIMARY

Changed to:
CREATE TABLE dbo.be_Categories(
CategoryIDuniqueidentifier ROWGUIDCOL NOT NULL CONSTRAINT DF_be_Categories_CategoryID DEFAULT (newid()),
CategoryNamevarchar(50) COLLATE SQLLatin1GeneralCP1CI_AS NULL,
CONSTRAINT PK_be_Categories PRIMARY KEY CLUSTERED
(
CategoryID ASC

) ON PRIMARY

Also anywhere there was (max) I changed to (ntext)

This all worked on my test box of Windows XP Server and SQL 2000.
I then moved everything over to Window 2003 Server and pointed to a SQL 2000 database where I now have my problem

I am sure its not a sql issue as it writes to the database fine and the website returns the post.

On your windows IIS 6 install are you going to a SQL 2000 or SQL 2005?


Thanks
Billyjack
Nov 18, 2007 at 8:37 PM
I just realized that this problem exist if I use xml or sql it lets me writhe the post then sends me to a page that doesn't exist.
Create post in title and put "blog xml" without the quotes, and then I save it sends me to.
http://myWebsite.org/dave/post/2007/11/blog-xml.aspx which doesn't exist.
Nov 20, 2007 at 4:19 AM
Just an update. I still have not gotten BE to work on the server that I was having an issue with. I copied BE from the one server and installed it on another server, added network services modify rights to the App_Data Folder. Everything worked fine.
Conclusion;
1. BE does work on Windows 2003 R2
2. There must be some setting on the two windows servers that are different.
3. Permissions were the same on App_Data Folder.

The only differences that I can see is the installation location:

Server 1
Windows 2003 R2
Installed BE in inetpub/dave
Created website not the Default location but an entire website.
Created Virtual Directory pointing to dave folder.
Set permissions to App_Data to network services modify, aspnet, modify, iusr, modify, iwam modify.
This installation did not work.

Server 2
Windows 2003 R2
Installed BE in inetpub/wwwroot/dave
Created Virtual Directory under inetpub/wwwroot pointing to dave folder.
Set permissions to App_Data to network services modify.
This installation did work.

Dec 5, 2007 at 3:21 AM
Hello Fellows,
So, I've the same problem. The server configuration is Windows 2003 - Standard and the URL: www.viniciuscamara.com.
I've tried all the tips above.

There are one post:
http://www.viniciuscamara.com/post/2007/11/Vista-da-minha-janela.aspx

And the page didn't exist.

I use in my blog only xml data files.

Someone can help me?
Thank you.
Jan 11, 2008 at 1:51 PM
Edited Jan 11, 2008 at 1:56 PM
Edited: I thought I had a solution, but unfortunately I haven't figured it out.

d.
Oct 30, 2008 at 2:19 PM
hello.

I tried the mod_rewrite rules but I got this:

Redirect Loop
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Oct 31, 2008 at 9:18 PM
Why re-invent the wheel?

As described on my blog  regarding the use of ManagedFusion’s URLRewriter