php - SEO friendly URL instead of query string .htaccess -
how can modify .htaccess file , seo friendly urls instead of query string. want achieve 3 goals:
- localhost/example/products/ instead of localhost/example/products-list.php
- localhost/example/products/38/ instead of localhost/example/products.php?id=38
- localhost/example/products/38/red/ instead of localhost/example/products.php?id=38&color=red
on post @anubhava helped me lot , have right second point:
rewriteengine on rewritebase /example # external redirect actual url pretty 1 rewritecond %{the_request} /products(?:\.php)?\?id=([^\s&]+) [nc] rewriterule ^ products/%1? [r=302,l,ne] # internal forward pretty url actual 1 rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f rewriterule ^products/([^/]+)/?$ products.php?id=$1 [l,qsa]
the second point works want know have put other rules, before or after. put right after other rule, it's not working because redirects previous url localhost/example/products/38/:
# external redirect actual url pretty 1 rewritecond %{the_request} /products(?:\.php)?\?id=([^\s&]+)\?color=([^\s&]+) [nc] rewriterule ^ products/%1/%2? [r=302,l,ne] # internal forward pretty url actual 1 rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f rewriterule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [l,qsa]
you need new set of rules 2 parameters:
rewriteengine on rewritebase /example/ # external redirect actual url pretty 1 rewritecond %{the_request} /products(?:\.php)?\?id=([^\s&]+)&color=([^\s&]+) [nc] rewriterule ^ products/%1/%2/? [r=302,l,ne] rewritecond %{the_request} /products(?:\.php)?\?id=([^\s&]+)\s [nc] rewriterule ^ products/%1/? [r=302,l,ne] rewritecond %{request_filename} -d [or] rewritecond %{request_filename} -f rewriterule ^ - [l] # internal forward pretty url actual 1 rewriterule ^products/([^/]+)/?$ products.php?id=$1 [nc,l,qsa] rewriterule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [nc,l,qsa] rewriterule ^products/?$ products-list.php [l,nc]
Comments
Post a Comment