1

I'm looking for series of .htaccess statements that will convert the following urls

http://mysite.com/product           to http://mysite.com/product.php
http://mysite.com/product/55            to http://mysite.com/product.php?id=55
http://mysite.com/category/38           to http://mysite.com/category.php?id=38
http://mysite.com/resources/car/19      to http://mysite.com/resources/car.php?id=19
http://mysite.com/resources/car/19?color=red&year=2013  to http://mysite.com/resources/car.php?id=19&color=red&year=2013

In other words, when rendering php files in my website, i want to drop the .php extension. If a url ends with a number, then i want to pass that as the id query string parameter. I also want to pass all the conventional query string parameters to my php my file like color and year.

I'm not sure how to construct such a .htaccess file.

ADDITIONAL NOTES I'm currently using hte following, but it fails to take into consideration urls that trail with a number, and passing that along as id

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} (.*)
RewriteRule . %{REQUEST_FILENAME}.php?%1 [L]

If I can do something like replace the trailing number in REQUEST_FILENAME in line two, thatwould be great.

John
  • 32,403
  • 80
  • 251
  • 422
  • did you try searching around? may be this helps! http://stackoverflow.com/questions/16230422/mod-rewrite-to-remove-php-extension-and-preserve-get-parameters – Eswar Rajesh Pinapala Oct 07 '13 at 03:08
  • http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ – Domecraft Oct 07 '13 at 03:10
  • Hey guys, i tried searching around but didn't find exactly what i was looking for. I updated my question to show my current rewrite rules. – John Oct 07 '13 at 03:25

1 Answers1

1

First you need to make sure Multiviews is turned off. Then you'll need 3 sets of rewrite rules:

Options -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)$ /$1.php?id=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^resources/([^/]+)/([0-9]+)$ /resources/$1.php?id=$2 [L,QSA]

You can be a little more specific if the URLs are actually just "product", "category" and "car", then you can just have:

Options -Multiviews
RewriteEngine On

RewriteRule ^product$ /product.php [L]

RewriteRule ^(product|category)/([0-9]+)$ /$1.php?id=$2 [L,QSA]

RewriteRule ^resources/car/([0-9]+)$ /resources/car.php?id=$1 [L,QSA]

John (the op) says:

This was the final .htaccess file i ended up with

RewriteEngine On
Options -Multiviews

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ $1.php?%1 [L]
John
  • 32,403
  • 80
  • 251
  • 422
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • What if I have a url like http://mysite.com/dir1/dir2/dir3/helloworld that needs to map to http://mysite.com/dir1/dir2/dir3/helloworld.php. Do I need write another set of RewriteRule for it? Basically everytime i add a new directory or subdirectory, i'll need write new rules? – John Oct 07 '13 at 03:25
  • @John if there is any arbitrary number of directories, you can get rid of the 3rd rule (that has the regex is `^resources/([^/]+)/([0-9]+)$`) then change the regex of the other two from `^([^/]+)` to `^(.*)`. – Jon Lin Oct 07 '13 at 03:27
  • great that worked. I also needed to make the second set of rules come first, or else i get an 500 internal server error. The logs said it was redirecting too many times. – John Oct 07 '13 at 03:40