regex - .htaccess - url rewrite that drops extension and passes argument -


i'm looking series of .htaccess statements convert following urls

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

in other words, when rendering php files in website, want drop .php extension. if url ends number, want pass id query string parameter. want pass conventional query string parameters php file color , year.

i'm not sure how construct such .htaccess file.

additional notes i'm using hte following, fails take consideration urls trail number, , passing along id

rewritecond %{request_filename} !-d rewritecond %{request_filename}\.php -f rewritecond %{query_string} (.*) rewriterule . %{request_filename}.php?%1 [l] 

if can replace trailing number in request_filename in line two, thatwould great.

first need make sure multiviews turned off. 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 little more specific if urls "product", "category" , "car", can 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 final .htaccess file ended 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] 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -