Mobile app version of vmapp.org
Login or Join
Shakeerah822

: 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

@Shakeerah822

Posted in: #Apache #UrlRewriting

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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Shakeerah822

2 Comments

Sorted by latest first Latest Oldest Best

 

@Shakeerah822

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

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

10% popularity Vote Up Vote Down


 

@Cofer257

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=

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme