Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

URL Rewriting with Parameters

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?

/product?id=1234565789&code=987654321&manufacturer=joe_blogs

The desired output in this example would be:

/product/123456789/987654321/joe_blogs


Thanks
0
Webbo_1980
Asked:
Webbo_1980
  • 2
1 Solution
 
Brad HoweCommented:
Hi,

First thing is to isntall IIS7 URL Rewrite 2.0
  http://www.iis.net/download/urlrewrite

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
http://your.domain.com/weblog/archive

the url will be converted, so that it will be as if they were:
http://your.domain.com/weblog/archive.aspx

Definition
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}


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

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

Further info for you. - http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Hope it helps clarify,
Hades666
0
 
Webbo_1980Author Commented:
Hello,
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:

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

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.
0
 
Webbo_1980Author Commented:
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now