Simple URL's

Simple Url's

This tutorial explains how to do simple urls like: http://www.domain.com/username Or http://www.domain.com/page_name.
It is different from doing Search Engine Friendly urls (like http://tutorial90.easycfm.com/) because this doesn't convert urls.
In a way it unconverts a url by taking advantage of a 404 missing page.

The concept should work for any server.
When a url like http://www.domain.com/username is viewed it obviously doesn't exist. Therefore it will display the 404 page.
On the 404 error page you will have code that deciphers the url and then displays the correct page.

You can set up a 404 error page one of two way.
First way is setting up a 404 missing page like 404.cfm on your server through your control panel or what ever you use.
The other way is adding similar code to your application.cfm file.
<cferror type="exception" template="/404.cfm">

On the 404.cfm page you will have similar code to this.

Example one: http://www.domain.com/username

<!--- Try converting url, Incase some one enters a weird url --->
<cftry>

<!--- You end up with a query_string that looks like this: 404;http://www.domain.com:80/username
You now need to remove "404;http://www.domain.com:80/" --->

<cfset cleanpath = #Replace(query_string, "404;http://www.domain.com:80/", "")#>
<!--- and incase some one doesn't enter www. --->
<cfset cleanpath = #Replace(cleanpath, "404;http://domain.com:80/", "")#>

<!--- Once removed you end up with: cleanpath = "username".
Now lets check to see if the user exists.--->

<cfquery name="UserCheck" DATASOURCE="database">
SELECT user_id
FROM users
WHERE users = 'cleanpath'
</cfquery>

<cfif UserCheck.recordcount NEQ 0>
<!--- Your include file might look like this. --->
<cfinclude template="profile.cfm?id=#cleanpath#">

<cfelse>
<!--- User doesn't exist. Dump them to an error page --->
<cflocation url="http://www.domain.com?error=y">
</cfif>

<!--- Incase of errors. You will always want the CFTRY in there in case a guest mistypes the url. --->
<cfcatch type="any">
<cflocation url="http://www.domain.com?error=y">
</cfcatch>
</cftry>

<!--- Understand what I did? --->

 

Example two: http://www.domain.com/page_name

Since you might not have a list of pages in your database. You could do some thing like this.

<!--- Try converting url, Incase some one enters a weird url --->
<cftry>

<!--- You end up with a query_string that looks like this: 404;http://www.domain.com:80/page_name
You now need to remove "404;http://www.domain.com:80/" --->

<cfset cleanpath = #Replace(query_string, "404;http://www.domain.com:80/", "")#>
<!--- and incase some one doesn't enter www. --->
<cfset cleanpath = #Replace(cleanpath, "404;http://domain.com:80/", "")#>

<!--- Once removed you end up with: cleanpath = "page_name". --->

<!--- Now lets see if the page exists. Use cftry because an error will just dump you back to this page. --->
<cftry>
<cfinclude template=
"#cleanpath#.cfm">
<cfcatch type="any">
<!--- If the page doesn't exist then dump the user to a different page --->
<cflocation url="http://www.domain.com?error=y">
</cfcatch>
</cftry>

<!--- Incase of errors. You will always want the CFTRY in there in case a guest mistypes the url. --->
<cfcatch type="any">
<cflocation url=
"http://www.domain.com?error=y">
</cfcatch>
</cftry>

<!--- Understand what I did? --->

The web site users and Bots will be none the wiser because the url will show up like http://www.domain.com/username.

The other good thing about this example is that it will also cover your normal 404 errors with a little extra work on the server end.

Performance notes: I don't know how this will affect server performance. The two examples above are rather simple and I would think
for smaller to mid size sites that you could make the code a little bit more complex without any issues. I would assume for larger
sites it might cause problems.

About This Tutorial
Author: Jeff Mendelsohn
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX7
Total Views: 57,041
Submission Date: March 19, 2007
Last Update Date: June 05, 2009
All Tutorials By This Autor: 3
Discuss This Tutorial
  • The whole foundation of your tutorial is shaky. You assume that Coldfusion will run the tag when there is a missing page. That is not true. At least, not in Coldfusion MX7 or Coldfusion 8. When Coldfusion cannot find a page, it either runs the missing-page-handler page that you registered in the Coldfusion Administrator or its own default error page. It will do that even when you include in Application.cfm.

  • I must be dumb! From what I see, this should work just fine. However, even though i set my coldfusion admin to set my missing template handler to /404.cfm, it didn't do anything. In fact, i get this page that just says 404/whatever the requested name is and on the address bar, it says JRun Servlet Error. Can anyone help me? SELECT UserID FROM Users WHERE Company = 'cleanpath'

  • @bill, SEO wise, cflocation is bad. Cflocation responds with a HTTP header status code 302, which means temporary redirect. In the age of phantom pages, bait-n-switch, duplicated content and other sleazy tactics to try to rank well most search engines don't like to see a lot of these. Instead use something like: This uses a permanent redirect, which tells the search engine that the old url really moved to this url, so they are considered one and the same.

  • I did a similar thing for SEO optimization of my URLS.. i'm wondering tho, is the redirect a problem.. my consultant wasn't TOTALLY sure, but said it could be.. so i'm doing includes which solves the problem, but i'm just curious.. anybody?

  • This is great. Will use it!!! Thanks heaps P.S. Secret word was cleavage. lol oh yeah!!!

Advertisement

Sponsored By...
Powered By...