Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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
share|improve this answer
    
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). –  Rowland Shaw Sep 10 '10 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. –  Lèse majesté Sep 10 '10 at 14:54
    
@Rowland: added a possible solution –  DisgruntledGoat Sep 11 '10 at 19:10
add comment
up vote 0 down vote accepted

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

RewriteRule ^products([^\.]+)$ products.php$1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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