Dynamic Rewrites

Topics: Developer Forum, Project Management Forum, User Forum
Nov 13, 2009 at 5:14 PM
Edited Nov 13, 2009 at 7:31 PM

Hello all,

I am using a CMS for ASP. I have decided to add a URL rewrite to the project but have been running into a few problems. I have a folder named content, this is where all of the public pages are. When a user creates a new page the rewrite rule should take the file name and use that as the path. An example is bellow.

RewriteRule ^/(.*) /Content/$1.asp

If I have a page named /Content/fishing.asp I would like to be able to input /fishing in the address bar and be directed to this page.

If I enable the rule bellow it works, but will not be dynamic rewriting.

RewriteRule ^/fishing /Content/fishing.asp

Let me know what the best solution to this problem is. Thanks!


Nov 13, 2009 at 7:26 PM

Hello Joshua

You can do file detection like this:

RewriteCond  %{APPL_PHYSICAL_PATH}\Content\$1.asp    -f
RewriteRule  ^/(.+)$   /Content/$1.asp   [L]

What that says is: rewrite any URL like "foo" to /Content/foo.asp . BUT! IIRF applies the rule only when the RewriteCond evaluates to true. In this case the RewriteCond uses the special operation "-f" , which is true when the previous argument is the name of an existing filesystem file.

%{APPL_PHYSICAL_PATH} is a server variable; it evaluates to the physical path of the IIS application. I think that server variable is set even when you're using an IIS website and not an "IIS Application".   If you're confused about what a "IIS APplication" is and how it is different from a "virtual directory" or a "web site" - you are not alone.  IIS has used some confusing terminology over the years.  See the article on iis.net that clears it up.   The short story is: make it an IIS Application - it costs you nothing and makes things simpler for the ISAPI Filter. 

So, that RewriteCond will be true when the file "<docroot>\Content\foo.asp" exists.  

Try it out and let me know how it works for you.