Archive

Posts Tagged ‘IIS’

ISAPI_Rewrite 3 – ignore existing files and directories

September 9th, 2008

In a recent project I needed to use search engine friendly urls. ISAPI_Rewrite 3 was on the IIS Windows server so I thought I’d use that instead of the crazy 404 custom cfm trick.

And it did work. But it also mapped the url for directories that actually existed, which is no good, it makes those dirs useless. So I had to put in conditions and rules to bypass that. I struggled with this problem for over two hours. So many examples and solutions online did not work.

After much trial and error, here is the resulting .htaccess file:

RewriteEngine on
RewriteBase /

#  Block external access to the Helper ISAPI Extension
RewriteRule ^.*\.isrwhlp$ / [NC,F,O]

# 301 Redirect all requests that don't contain a dot or trailing slash to include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [S=3]
RewriteRule ^(.*)/(.*)/(.*)/$ /includes/cfm/sefurl.cfm?sec=$1&top=$2&pag=$3 [QSA]
RewriteRule ^(.*)/(.*)/$ /includes/cfm/sefurl.cfm?sec=$1&top=$2 [QSA]
RewriteRule ^(.*)/$ /includes/cfm/sefurl.cfm?sec=$1 [QSA]

There might be a better way to do this, as this was my first experience with rewrite.

ColdFusion