2

Given a (slightly theoretical) physical disk layout of:

\products\cameras\50d.jpg
\products\cameras\20d.jpg
\products\lenses\18-55.jpg
\products\lenses\28-135.jpg
\products.php

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

/products.php/
/products.php/cameras/
/products.php/cameras/50d

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

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
3
  • 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). 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. Sep 10, 2010 at 14:54
  • @Rowland: added a possible solution Sep 11, 2010 at 19:10
0

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 that you have read and understand our privacy policy and code of conduct.

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