1

How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:

  1. localhost/example/products/ instead of localhost/example/products-list.php
  2. localhost/example/products/38/ instead of localhost/example/products.php?id=38
  3. localhost/example/products/38/red/ instead of localhost/example/products.php?id=38&color=red

On another post @anubhava helped me a lot and this is what I have right now for the second point:

RewriteEngine on
RewriteBase /example
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [L,QSA]

The second point works properly but I want to know where do I have to put the other rules, before or after. I put this right after the other rule, but it's not working because it redirects to the previous url localhost/example/products/38/:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\?color=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [L,QSA]
  • While asking a question in the forum, you should be pleasing and the question should be followed by your effort which you have given so that some one will help. I have updated the question for you. – Vidya Sagar Jul 02 '15 at 09:22
  • @Sagar: ...and I rolled it back because you didn't improve the question, and you pointlessly added a "please help me" style outro – musefan Jul 02 '15 at 09:23
  • Thanks for editing my question. On other posts I gived thanks in advance and somebody deleted it, thought that it was wrong since that. – Estirpicidades Jul 02 '15 at 09:30
  • @Estirpicidades: It is wrong, you shouldn't have put it back in. Instead of going with Sagar's useless improvement, how about you edit it with some meaningful information, specifically try something and show your efforts. You won't get any help if you leave it like this, it just isn't specific enough – musefan Jul 02 '15 at 09:37
  • Getting there... now explain what is wrong with the code you have posted. I mean, what result is it giving you that is different from what you want to achieve – musefan Jul 02 '15 at 09:42
  • possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – deceze Jul 02 '15 at 09:51
  • 2
    Thanks @musefan and sorry for my question, I'm new here, i'll try to improve. – Estirpicidades Jul 02 '15 at 10:01

1 Answers1

6

You need new set of rules for 2 parameters:

RewriteEngine on
RewriteBase /example/

# external redirect from actual URL to pretty one
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 from pretty URL to actual one
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]
anubhava
  • 761,203
  • 64
  • 569
  • 643