• Notice regarding image hosts and attachments. See here.

URL Re-writing

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Have been meaning to do some url rewriting for ages now and am finally going to sit down tomorrow and do some :)

Have been reading a few tutorials and still have a question :(

Say I make a coupe of pages, a master page that lists a few products and a detail page for a product.

What I want to end up with is a simple list of urtls something like www.domain.com/products/2/product-name.php instead of www.domain.com/products.php?prodID=2&name-yadada

Would I code the pages as normal (E.g. having say the product ID in the querystring) and then apply the rules in the htaccess file? After I have uploaded the new htaccess file the pages urls will automatically be re-written before being sent to the browser.

Am I on the right track, or have I missed the point? :)

Thanks
 
What language are you writing this in suarve?

I've noticed you've used classic ASP before, and in that case you'll need to a write a custom 404 page for IIS.
 
What language are you writing this in suarve?

I've noticed you've used classic ASP before, and in that case you'll need to a write a custom 404 page for IIS.

It's mostly PHP now. Could use ASP if I wished, but PHP is a better language to work in once you get used to the syntax differences imo. :)
 
ok, been having a quick go of this.

To test i made a simple page that gets a value from the query string - product.php?ID=23. Using the below rule I got the following URL working: domain.com/product/33 :)

RewriteEngine on
RewriteRule ^product/([0-9]+)$ product.php?ID=$1 [NC,L]

Have a few more questions that I can't the answer to anywhere:

Say the ID didn't exist or was in the wrong format, you could redirect to a different page using PHP. However, say I enter /product/asdfgahf the page is not found.

I've seen a lot of pages using something like /catname/productname to rewrite urls too. Does this mean for every page I'll have to pass the category name and product name across via the querystring? Also, what happens to say the cat and product IDs when I do this, as I'd normally use those E.g. product.php?cat=44&prod=12.

Thanks
 
I've seen a lot of pages using something like /catname/productname to rewrite urls too. Does this mean for every page I'll have to pass the category name and product name across via the querystring? Also, what happens to say the cat and product IDs when I do this, as I'd normally use those E.g. product.php?cat=44&prod=12.

Not everyone uses modrewrite to do this, for a full blown web application such as the ones written to run e-commerce sites they are probably doing it in PHP. Modrewrite would be used to redirect all requests to a single script called the 'Front Controller' such as index.php. From there the PHP would take care of the routing internally by deconstructing the URL, this is the approach that several web frameworks use such as Zend. It allows tigher controls over things like error pages, for example if the target page doesn't exist the script redirects you to the nearest index (eg /products). It can also allow easy implementation of access control.

I'm not saying that it can't be done using modrewrite but it might be worth considering the alternatives.
 
Last edited:
Back
Top Bottom