Map Directory to Query String Parameter

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 11
Joined: 20 Aug 2012, 14:37

Map Directory to Query String Parameter

01 Apr 2014, 18:16

I need help with a rule that maps the first directory into a query string parameter, but leaves my image and assets alone.

When I test the expression in the RegTest tool, it displays as I would expect. However, when executed on the actual server it says, "The page cannot be found"

My Complete file:
Code: Select all
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.79

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(?!.*\.(?:gif|jpg|png|css|swf|link|script|js))(.+)/?([0-9]*)/?    default.aspx?page=$1 [I,L]


Here are some tests and how they should map.
sub.example.com/foo -> sub.example.com/default.aspx?page=foo

sub.example.com/bar -> sub.example.com/default.aspx?page=bar

sub.example.com/foo-bar -> sub.example.com/default.aspx?page=foo-bar

sub.example.com/images/logo.gif -> sub.example.com/images/logo.gif

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Map Directory to Query String Parameter

01 Apr 2014, 22:13

Hello,

I think the issue is pretty simple - a slash in front of the pattern. So all you need is:
Code: Select all
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!.*\.(?:gif|jpg|png|css|swf|link|script|js))(.+)/?([0-9]*)/?    default.aspx?page=$1 [I,L]


However, I'd use:

Code: Select all
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^[^\.]\.(?:gif|jpg|png|css|swf|link|script|js) [NC]
RewriteRule ^([^/]+)/?.*  /default.aspx?page=$1 [I,L]


Regards

User avatar
Posts: 11
Joined: 20 Aug 2012, 14:37

Re: Map Directory to Query String Parameter

02 Apr 2014, 09:30

Thanks! I went with this one:

RewriteRule ^([^/]+)/?.* /default.aspx?page=$1 [I,L]

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 0 guests