Blog

Brad Wood

December 24, 2016

Spread the word


Share your thoughts

Christmas baking has probably began in your house, filling the air with the sweet aroma of sugar, carbs, cholesterol, and partially hydrogenated oil.  Mmm, just like Grandma used to make!  What better time to cook up a new development environment that runs on CommandBox.  

URL rewriting is common on many server setups and while everyone does the same basic things, the config files they use vary greatly based on the web server they have in play.  CommandBox allows for URL rewriting, but a common question I get is how to convert the rewrites people already have on their existing web servers so CommandBox can use them.

Converting Apache and IIS URL Rewrites

This post is long on examples so I'll try and be short on commentary.  CommandBox uses a Java URL Rewriting engine called Tuckey whose default config is an XML format.  Let's look at the main scenarios you're probably dealing with.  

No special rewrites

If you don't have any custom rewrites, but you want to use a framework like ColdBox MVC that allows SES urls that need the index.cfm added back in, this is the easiest scenario.  Simply start your server with the --rewritesEnable flag (which will save to your server.json after the first time).  

CommandBox> start --rewritesEnable

This will enable our basic out-of-the-box rewrite rule that turns URLs like site.com/foo/bar into site.com/index.cfm/foo/bar

If you want to take a look at what the configuration for the default rewrites looks like, check out this file: ~/.CommandBox/cfml/system/config/urlrewrite.xml

Apache mod_rewrite

You may be using Apache's mod_rewrite for all your rewriting needs.  Well guess what-- Tuckey has built in support for the mod_rewrite syntax!  All you need to do is put your rewrite rules in an .htaccess file (which you may already be doing) and specify that file as the custom rewrite config file.  Note the filename must be .htaccess.

CommandBox> start --rewritesEnable rewritesConfig=.htaccess

Tuckey will pick up your file and use it.  If it doesn't seem to be working, run the server log command and see if there's any errors being thrown from Tuckey.There are a few documented differences between the original mod_rewrite library which you can read about here: 
http://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html#mod_rewrite_conf

IIS Rewrites

IIS has its own rewrite engine that uses an XML format that is actually pretty similar to Tuckey's XML format.  Some CF devs from Slack gave me the following IIS rewrite rules to use an example to convert for CommandBox use.  A quick read through the Tuckey docs and you'll be familiar with its syntax and options.  I'm only going to show the full Tuckey XML file in the first example.  The rest will just be the rule fragment to save space.  Save the rewrite rules in an XML file and specify them just like the .htaccess rules above, but with the correct filename.  Please note, Tuckey expects the leading slash in your request URI regex unlike IIS. 

IIS Rule

<rule name="Rewrite Library Genre Name " enabled="true" stopProcessing="true">
    <match url="^Library/Genre/([^/]+)$" />
    <action type="Rewrite" url="Library/Genre/index.cfm?name={R:1}" />
</rule>

CommandBox Rule (Full XML file)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>	
	<rule enabled="true">
		<name>Rewrite Library Genre Name</name>		
		<from>^/Library/Genre/([^/]+)$</from>
		<to type="passthrough" last="true">/Library/Genre/index.cfm?name=$1</to>
	</rule>
</urlrewrite>

IIS Rule

<rule name="Rewrite siteMap" enabled="true">
    <match url="^sitemap.xml" />
    <action type="Rewrite" url="sitemap.cfm" />
</rule>

CommandBox Rule

<rule enabled="true">
    <name>Rewrite siteMap</name>
    <from>^/sitemap.xml</from>
    <to type="passthrough">/sitemap.cfm</to>
</rule>

IIS Rule

<rule name="Canonicalize Home Page">
    <match url="^index\.(?:htm|html|cfm)" ignoreCase="true" />
    <action type="Redirect" url="http://www.domain.com/" redirectType="Permanent" />
</rule>

CommandBox Rule

<rule>	
    <name>Canonicalize Home Page</name>
    <from casesensitive="true" >^/index\.(htm|html|cfm)</from>
    <to type="permanent-redirect">/</to>
</rule>

IIS Rule

<rule name="Canonicalize Domain" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="www\.domain\.com" />
        <add input="{HTTP_HOST}" negate="true" pattern="localhost" />
        <add input="{HTTP_HOST}" negate="true" pattern="127.0.0.1" />
    </conditions>
    <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" appendQueryString="false" />
</rule>

CommandBox Rule (Added port)

<rule>
    <name>Canonicalize Domain</name>
    <condition name="host" operator="notequal">^www\.domain\.com</condition>
    <condition name="host" operator="notequal">^localhost</condition>
    <condition name="host" operator="notequal">^127\.0\.0\.1</condition>
    <from>^(.*)</from>
    <to type="permanent-redirect" qsappend="true" last="true">http://www.domain.com:%{port}$1?</to>
</rule>

IIS Rule

<rewriteMaps>
    <rewriteMap name="301Redirects">
        <add key="/oldPage.cfm" value="/newpage.cfm" />
        <add key="/something.cfm" value="/somethingElse.cfm" />
        <add key="/faq.cfm" value="/support.cfm" />
    </rewriteMap>
</rewriteMaps>
<rules>
    <rule name="301 Redirects">
        <match url=".*" />
        <conditions>
            <add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Redirect" url="{C:1}" redirectType="Permanent" />
    </rule>
</rules>

CommandBox Rules

<rule>
    <from>^/oldPage.cfm</from>
    <to type="permanent-redirect">/newpage.cfm</to>
</rule>
<rule>
    <from>^/something.cfm</from>
    <to type="permanent-redirect">/somethingElse.cfm</to>
</rule>
<rule>
    <from>^/faq.cfm</from>
    <to type="permanent-redirect">/support.cfm</to>
</rule>

 

Add Your Comment

Recent Entries

BoxLang 1.0.0 Beta 7 Launched

BoxLang 1.0.0 Beta 7 Launched

We are pleased to announce the release of BoxLang 1.0.0-Beta 7! This latest beta version includes improvements and essential bug fixes, but more importantly it certifies the execution of ColdBox HMVC and TestBox.

What is BoxLang?

BoxLang is a modern dynamic JVM language that can be deployed on multiple runtimes: operating system (Windows/Mac/*nix/Embedded), web server, lambda, iOS, android, web assembly, and more. BoxLang combines many features from different progr

Luis Majano
Luis Majano
July 26, 2024
New BoxLang Feature: Java Method References and Higher-Order Functions

New BoxLang Feature: Java Method References and Higher-Order Functions

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

Maria Jose Herrera
Maria Jose Herrera
July 26, 2024
Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Are you a CFML developer looking to take your skills to the next level? Look no further than the ColdBox from Zero to Hero Virtual Live Training! This intensive two-day course will equip you with the knowledge and expertise to build robust and scalable applications using ColdBox 7, the latest version of the most popular CFML MVC framework.

What You'll Learn:

  • Master the Fun...

Cristobal Escobar
Cristobal Escobar
July 24, 2024