I just launched my new website this week and while I am happy there are still some things I want to clean up. Today we are going to take a look at cleaning up the urls in my blog a little bit. While I haven't pushed this into production at this time I have tested it on my local box so I thought I would share it with everyone. Let me stress here that I suck at rewrite rules and I am sure someone can point out a mistake here. So if you have comments or suggestions on how to do this better please speak up.
The urls for BlogCFC are pretty friendly out of the box but I think we can improve them a bit. Really when it comes down to it we are talking about 2 urls, the entry and category links. We want to take our links and strip the index.cfm out of them but it still needs to work with the index.cfm for links that already exist. Here is an existing entry and category link. Those URLS are ok but I want them to look like this
To accomplish this we actually need to create some rewrite rules and understand how BlogCFC works. First we will take a look at the rewrite rules, and more specifically mod_rewrite rules. Let's break these rules down starting with the entry. Before your actual rewrite rule you can make sure that a condition passes, think of it like a where clause in a sql statement. In this case we are using a special variable %{REQUEST_FILENAME} which is the full local filesystem path to the file or script matching the request. This will usually contain everything after host so in my case everything but http://www.danvega.org/. With that we are using the conditions -f and -d which check to make sure its an actual file or an actual directory. We are telling the condition that we want to make sure these DONT pass, or not a directory or logical file. For the rewrite rule we are doing a few things. First we are making sure that it starts with blog/. In the next capture we are matching some regex. Basically we want to make sure its in the format of /blog/4 digit number/1 or 2 digit number/1 or 2 digit number/*(anything) Now that we have made a match we can rewrite the url to blog/index.cfm/$1. The $1 is basically everything we matched in our regular expression. Finally we add the flag QSA which stands for query string append. This just means that if there was a query string on the request, add it to the rewrite.
So that is the bulk of the work but there is still 1 step to go. BlogCFC has a setting in the blog.ini.cfm file for blogURL. Right now it probably looks something like this This is the url that blogcfc will use internally when you call functions like makeLink() or makeCategoryLink(). So to work with our new urls you will want to update that url and remove the index.cfm. That was not nearly as tough as I thought it was going to be. Like I said earlier, I don't have this in production yet but I plan to commit the code in the next couple of days. If you have any suggestions on how to improve these rewrites please let me know. As I said at the start of this, I stink at mod_rewrite rules and regex and its something I am trying to get better at.
Update: After committing the changes tonight I noticed one other issue. The captcha for the add comment was not showing up. The reason for this is pretty clear when you look at the add comment form. The url in our blog settings needs to be without the trailing / because of the way links are generated so I just updated this form by adding the / #application.blog.getRootURL()#/showCaptcha.cfm. I suspect other small issues might pop up so if you see any please let me know.
Update:The other downer is that now that the links are technically different (no index.cfm) all of my facebook likes / tweets are reset to 0. booooooo
Update: I found another issue this time again with the way the url was being built in the email that gets sent after a comment has been place. I am not going to keep listing these out here but there seems to be inconsistencies on the way the full urls are being built. None of the links in templates use the / but methods that build the urls do.
