Given a (slightly theoretical) physical disk layout of:


At the moment, I've URLs of the form:


With the products.php using the PATH_INFO to make a decision on what to display.

I'm struggling to rewrite the URL to remove the .php, but still allow static resources to be retrievable though?

2 Answers 2


Firstly all those back-slashes must be forward-slashes. The \ is used to "escape" special characters, not for folder separation.

Rewriting products.php to products will not work if you have a products folder already. I'd suggest moving all the images to a different directory like /images/cameras/50d.jpg. Then you can do this:

RewriteRule ^products products.php

With that you will need to parse the requested URL in PHP to find the variables. You could also use this:

RewriteRule ^products/([^/]+) products.php?cat=$1
RewriteRule ^products/([^/]+)/([^/]+) products.php?cat=$1&prod=$2

Now in your PHP script you automatically have the required variables via $_GET['cat'] and $_GET['prod'].

EDIT: actually you're right, it is possible to have both, as long as all the images are limited to specific extensions. I think this will work:

RewriteRule ^products/(.+).jpg products/$1.jpg [L]
RewriteRule ^products/([^/]+) products.php?cat=$1
RewriteRule ^products/([^/]+)/([^/]+) products.php?cat=$1&prod=$2
  • Would it not be possible to have a rule that kept the static resources where they are? (in reality, this is to replace some a poor man's approach, where there used to be a page per product/category which all called down to a shared library). Commented Sep 10, 2010 at 12:50
  • @Rowland Shaw: You can keep the /products directory, but that means you won't be able to use /products to display a page. There will always need to be a category and product parameter in the URL, e.g. /products/lenses/18-55, otherwise Apache will serve up the folder rather than the script by default. Commented Sep 10, 2010 at 14:54
  • @Rowland: added a possible solution Commented Sep 11, 2010 at 19:10

Turns out that this works, and still allows resources within to work.

RewriteRule ^products([^\.]+)$ products.php$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.