.htaccess rewriting

advertisements

I have this .htaccess that I've been using to rewrite URLs like these:

www.example.com/index.php?page=brand          www.example.com/brand
www.example.com/index.php?page=contact        www.example.com/contact
www.example.com/index.php?page=giveaways      www.example.com/giveaways

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

I used a file called index.php to handle the redirects. Code used below:

$page = trim($_GET['page']);
if($page == "giveaways")
    require('pages/giveaways.php');

Now, I would like to add another URL type like these:

www.example.com/index.php?page=products&p=ford-mustang

TO

www.example.com/products/ford-mustang

How will I accomplish this? Any suggestion would be greatly appreciated.


You need to add a second RewriteRule above your current one. Here's an example:

RewriteRule ^/(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]

This will rewrite /product/ford-mustang to index.php?page=product&p=ford-mustang

Remember to add it above your current RewriteRule, because it first tries to match the first RewriteRule, when there's no match it will go on with the second RewriteRule and so further.