URL Rewriting with Parameters

Posted on 2011-05-03
Last Modified: 2012-06-27
We need to display products that have 3 parameters with a friendly URL - what's the correct regular expression to turn the following into something more usable?


The desired output in this example would be:


Question by:Webbo_1980
    LVL 30

    Accepted Solution


    First thing is to isntall IIS7 URL Rewrite 2.0

    Rewrite allows you to create rules on the fly. Take the following example for an extentionless url rewrite.

    RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.aspx [L]

    For example, if someone visits the URL

    the url will be converted, so that it will be as if they were:

    Rewrites are a Server-side operation. The browser is not notified that a rewrite has occurred, inside the web server.
    Redirect sends an immediate response to the Client. A redirect implies sending an HTTP response to the client, with a new URL.

    Now, this is a sample rule for a friendly rewrite for your example.

    MATCH       ^product/([^/]+)/([^/]+)/([^/]+)/?$
    REWRITE  product?id={R:1}&code={R:2}&manufacturer={R:3}

                    <rule name="Dynamic Friendly URL /product" stopProcessing="true">
                        <match url="^product/([^/]+)/([^/]+)/([^/]+)/?$" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <action type="Rewrite" url="product?id={R:1}&amp;code={R:2}&amp;manufacturer={R:3}" />

    1. user enters /product/123456789/987654321/joe_blogs
    2. user receives  /product?id=1234565789&code=987654321&manufacturer=joe_blogs

    Further info for you. -

    Hope it helps clarify,

    Author Comment

    Thanks for your reply and apologies for the huge delay.  I've followed your example and still can't get this working.

    You mentioned that:

    the pattern is: ^product/([^/]+)/([^/]+)/([^/]+)/?$
    and the rewrite url is: product?id={R:1}&amp;code={R:2}&amp;manufacturer={R:3}

    but to me it seems like that would be backwards?  Especially as when I use the 'test pattern' tool in the URL Rewrite module, entering "product?id=7&code=9780001712843&manu=berenstain_jan" doesn't match the pattern, but entering "product/123/321/abc" does match.

    In any case, the code produced in the config is as follows:

    				<rule name="Product" stopProcessing="true">
    					<match url="^product/([^/]+)/([^/]+)/([^/]+)/?$" />
    					<action type="Rewrite" url="product?id={R:1}&amp;amp;code={R:2}&amp;amp;manu={R:3}" />

    Open in new window

    The &amp;amp; doesn't look right to me.  Can you see what I've done wrong?

    Many thanks and again apologies for the delay.

    Author Comment

    I've tried your suggestion in a blank solution and it seems to work fine, so there must be something in our project that's affecting it.  Thanks for the answer tho - it did help me come to some conclusions.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now