1 Reply Latest reply on Oct 12, 2007 4:55 PM by romeogq

    Dynamic to Static URL with a twist...

      Ok, I am trying to get my website to display a unique web adderss for each username in my SQL database. For example:
      username "testusername" would pop up a website entitled "www.example.com/testusername".

      I anticipate having thousands of unique usernames, and instead of creating a new, identical web page for each person, it seems like there must be a way to display one page dynamically without actually typing in a query string for only one variable.

      I will be setting a session variable based on the unique username entered in the URL, so I guess there are two things I need to know:
      1. how can I get a URL that doesn't technically exist to not throw a 404, but instead display the same index page dynamically?
      2. (and this may be answered in answering #1) how can I pull the username entered in the URL for use in querystings for setting session variables?

      I hope I made some sense... any help would be WONDERFUL!
        • 1. Dynamic to Static URL with a twist...
          romeogq Level 1
          I did this for a client by simply modifying the 404 error page. You do not have to install any special renaming software onto your server whatsoever.

          First create a custom 404 error page outputting the cgi.query_string of the error page. In that you will see the incorrectly called url, which in your case will include the unique username.


          Extract the username using a combination of your list functions...

          <cfset username = listdeleteat(listgetat(CGI.QUERY_STRING, 3, ":"), 1, "/") />

          Remove the trailing slash if needed...

          <cfset username = listgetat(username, 1, "/") />

          Then cfquery the username. If found, cfinclude the relevant index page (if you want the url to stay the same), or cflocation to the actually page assigning the username as a url variable. If the username is not found, just goto home page.

          Remember to disallow any special characters or top level directory names in your usernames.

          <cfloop condition='findoneof("\/:*?""<>|", form.urlname)'>
          <cfset form.urlname = removechars(form.urlname, findoneof("\/:*?""<>|", form.urlname), 1) />

          <cfdirectory action="list" name="alldirs" directory="#expandpath('/')#" />
          <cfquery dbtype="query" name="dirs">
          SELECT name
          FROM alldirs
          WHERE type = "Dir"
          AND name = #lcase(form.urlname)#