: Removing the script extension with URL rewriting on Apache Given a (slightly theoretical) physical disk layout of: productscameras50d.jpg productscameras20d.jpg productslenses18-55.jpg productslenses28-135.jpg
Given a (slightly theoretical) physical disk layout of:
productscameras50d.jpg
productscameras20d.jpg
productslenses18-55.jpg
productslenses28-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?
More posts by @Shakeerah822
2 Comments
Sorted by latest first Latest Oldest Best
Turns out that this works, and still allows resources within to work.
RewriteRule ^products([^.]+)$ products.php
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=
RewriteRule ^products/([^/]+)/([^/]+) products.php?cat=&prod=
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/.jpg [L]
RewriteRule ^products/([^/]+) products.php?cat=
RewriteRule ^products/([^/]+)/([^/]+) products.php?cat=&prod=
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.