Skip to main content
Inspiring
April 8, 2011
Question

url rewriting

  • April 8, 2011
  • 2 replies
  • 15491 views

If I have a dynamic page that loads content depending on a url parameter (whatever.com/editorial.cfm?id=35), how can I rewrite that to make sense? For every record, I do have a "title" for the editorial, like "The best eateries in town" for instance. Any way to use that in the url instead of the id and still have the id recognized on article.cfm?

    This topic has been closed for replies.

    2 replies

    Inspiring
    April 8, 2011

    So what you're saying is you want an URL like this:

    http://whatever.com/editorial/The_best_eateries_in_town/

    I agree this is best done via some sort of ISAPI_rewrite module, and not via CF.

    How many articles have you got?  One way to do this would be to generate a rewrite map file which maps the article titles to their IDs, eg:

    The_best_eateries_in_town 35

    The_best_cinemas_in_town 36

    #etc

    (that's be the syntax for mod_rewrite, I dunno how IIS7 handles these things, but know that it does handle them (from my 15sec worth of googling, just now)).

    Rewrite maps scale pretty well, so having a mapping for each article - provided you don't have millions of them - would be fine.  You could regenerate this file from your DB periodically, or whenever an article is added / updated / deleted you could run an event handler that updates or recreates the map file.

    Then it's just a matter of rewriting your URL from the human-friendly one to the one CF will expect.  It it was mod_rewrite that'd be something like:

    RewriteMap title2ID txt:title2ID.map
    RewriteRule ^/editorial/(.*)/ /editorial.cfm?id=${title2ID:$1}

    IIS's rewriting engine seems to be all point-and-clicky-based, not .htaccess-based, so dunno how it effects the same thing, but that's pretty much what you want to do.

    If you have too many articles to sensibly maintain a map file, then you're gonna need to include the article ID in the URL, thus:

    http://whatever.com/editorial/The_best_eateries_in_town/35/

    And use a rewrite to grab the "35".  This approach is probably easier, but makes for a slightly less friendly URL (although who really cares?  Who makes any judgements based on the URL... and that'll still be fine for SEO).

    --

    Adam

    Owainnorth
    Inspiring
    April 8, 2011

    I've had need to use the IIS rewrite module recently, and it is very good. As Adam says it does have an interface, but all that does it help you create the web.config file - you can create this locally then just dump it in your web root if you don't have access to the server.

    By far the most extensible and future-proof method is to include the Subject Id within the url somewhere so you don't have to map everything out. Look at this Amazon link:

      http://www.amazon.co.uk/Practical-Whispering-Threshold-picture-guides/dp/1872119670/ref=sr_1_1?ie=UTF8&qid=1302248856&sr=8-1

    Spot the Id in there? 1872119670. They'll be ignoring everything else in that link (except url parameters) and just using the id to get the data from the database.

    I've just tested a rule (I can't remember the exact regex so this one might be a bit shanty) which will allow rewriting of urls on a fairly basic level, but that needs no modifying when you add new first-level pages. The web.config looks like so:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="GenericSubjectId">
                        <match url="^(\w*)/([0-9]+)?/(.*?)/?$" />
                        <action type="Rewrite" url="/{R:1}.cfm?SubjectId={R:2}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

    What it actually does is matches any url that starts with a word, then a forward slash, then a numeric id, then ignores the rest. This means that from the back of this rule, if I create a link to, say:

      /horses/938734/my_biggest_horse_ever/something/pics

    Then it actually requests the page "/horses.cfm?SubjectId=938734" from ColdFusion. Likewise, if I go to:

      /people/01812/muppets/family

    Then I actually get served "/people.cfm?SubjectId=01812".

    That's what I'd do if you're not going to be doing too much with subfolders, but even then you can just amend the rule accordingly. Quick-and-dirty one-time-only setup for basic url rewriting!

    For. The. Win.

    Squiggy2Author
    Inspiring
    April 8, 2011

    Wow, thanks for all of the suggestions! My host does have the mod rewrite component on the server, but I was just thinking... let me know if this is a lame direction to go:

    When creating an article (which comes from a file uploaded to the server), my client inputs the editorial title. What if I also have him type in a search engine-friendly name, like "Backyard-remodeling-made-easy" and then I add that to the url, like "websitename.com/article.cfm?Backyard-remodeling-made-easy&articleID=335"

    Would that be the best of both worlds or would the search engines just giggle at a bad attempt to create a readable string for them?

    Known Participant
    April 8, 2011

    Does your server have any form of a "URL Rewriter" tool available?  Is it a Windows or Linux server?

    Squiggy2Author
    Inspiring
    April 8, 2011

    Windows: Microsoft-IIS/7.5

    I've looked around for a url rewriting tool from the host, but if their forum is any indication, it doesn't exist.

    Owainnorth
    Inspiring
    April 8, 2011

    IIS7 has its own rewrite engine, if it's not installed (it's an addon) I'd ask the host if they can install it for you; it's an official Microsoft add-on and doesn't cause any problems. I work for a hosting company and we always install it by default now. Once installed, you can configure your rewrite rules using a web.config file in your web root, so you don't need to contact your hosts to make changes.

    Trying to do rewriting in CF is a massive pain, with lots of potential issues with error and 404 handlers and even then it'll only handle pages it's given, such as .cfm, .cfml etc; it won't be able to rewrite any non-CF requests, such as .htm etc.

    Rewriting is *definitely* something to be done at the webserver level.