Posts Tagged ‘Apache’

Apache, mod_rewrite and Sexy URLs

Tuesday, June 16th, 2009

99% of the people who visit a web site don’t care how it works, or how it was built. They don’t care if you’re using ColdFusion or PHP, and they don’t care if you’re using post or get variables. The remaining one percent comprises some techie types like me, and probably you, and some people with less than honourable intentions. For members of the latter group, you don’t WANT them to know what type of technology you use.

This is a good argument for URL rewriting, as is the fact that semantic and descriptive URLs assist in search engine ranking, and general usability. I rewriting my URLs because I like the way they look. I think a website looks much more polished when the URLs are descriptive, free of variable/value pairs, and as short possible.
(more…)

Posts Tagged ‘Apache’

Apache, mod_rewrite and Sexy URLs

Tuesday, June 16th, 2009

Instead of using all the URL shortening services out there, I thought it would be just as easy to write my own, and retain my own domain in the URL. My domain name is not that short, but since I would only be using it to redirect to my own pages, I would only need a few characters to create the links. I use Apache as a webserver, and Mango Blog as an engine. This method, however, also works with BlogCFC, another popular, and awesome, ColdFusion blog engine. Both these blog engines use UUIDs as primary keys in the blog entry tables. I added an integer field in the entry table called “pk”, and made it the primary key with auto-increment turned on. This gives all your blog entries a numeric index. The next step is to create a file somewhere on your site with some code to redirect requests that use your URL shortening service. I called it trimURL.cfm:

1
2
3
4
5
6
7
8
9
10
11
<cfquery name="getURL" dataSource="mallinson">
  SELECT    name
  FROM 	 entry
  WHERE	 pk = <cfqueryparam CFSQLType="CF_SQL_VARCHAR" value="#url.id#" />
</cfquery>
 
<cfif getURL.recordcount>
	<cflocation addToken="no" url="http://mallinson.ca/post/#getURL.name#" />
<cfelse>
	<cflocation addToken="no" url="http://mallinson.ca" />
</cfif>

With this code, this blog entry can be referenced by visiting http://mallinson.ca/12. The next step requires that you know a bit about Apache Web Server, or are familiar with URL rewriting using another type of web server. I use Apache, and URL Rewrite. In my .htaccess file, I have a lot of entries that clean up my URLs, but I’ll isolate the one rule that makes the URL rewriting work.

RewriteEngine On
RewriteRule ^([0-9]{1,6})$ trimURL.cfm?id=$1 [L]

What this bit of code does is take any URL that is composed entirely of digits, up to 6 digits (I don’t anticipate breaking a million posts), isolate the digit, and pass it to my redirecting page. I intend to register another much shorter domain as soon as I find something good. Then I could get the URL strings down to 10 characters or so.

Bio

A Web Developer in Vancouver who has been playing with computers since the dawn of time.