views:

15

answers:

1

Hi all,

I would like to use following RESTful URL for a web site I'm working on.

http://mysite.com/Products/category=bags&colours=black

can anyone please tell me how to achieve this with .htaccess?

Oscar

+1  A: 

Here's an .htaccess to capture anything except when files or directories conflict and it routes to /index.php:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

With that we can use /index.php as our request router. In PHP we simply get the request something like so:

<?php
$url_path = $_SERVER['REQUEST_URI'];
// Parse your URL path here...

That said, your URL pattern is non-standard and I'd really recommend against it because proxies and browsers will URL encode the ampersand and equal signs. Let me suggest one of the following instead (and also to use lowercase as it helps with validation of the URL):

http://example.com/products/categories/bags/colours/black
http://example.com/products/category_bags/colours_black
http://example.com/products?category=bags&amp;colours=black
http://example.com/products/categories/bags?colours=black
http://example.com/products/bags?colours=black

To see what will encode in the path of a URL just Google it and look at Google's search URL.

Hope this helps.

-Mike

MikeSchinkel