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]
7
  • 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. Commented Jul 2, 2015 at 9:22
  • @Sagar: ...and I rolled it back because you didn't improve the question, and you pointlessly added a "please help me" style outro Commented Jul 2, 2015 at 9: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. Commented Jul 2, 2015 at 9: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 Commented Jul 2, 2015 at 9:37
  • 2
    Thanks @musefan and sorry for my question, I'm new here, i'll try to improve. Commented Jul 2, 2015 at 10:01

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.