1

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.

1
  • ok, pls edit question for removing downvote Commented Jun 29, 2011 at 13:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply! I used this code: RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA] RewriteRule ^(.*)$ index.php?page=$1 [L,QSA] The result is somehow okay but my CSS is not working. What could be caused this problem?
if your CSS is in a folder, you can add a rule to ignore it... RewriteCond %{REQUEST_URI} !^/css\.* [NC] would ignore everyting in domain.com/css add this BEFORE the RewriteRule
That's because the path to your css file is relative, something like this: stylesheets/style.css, then when you have such RewriteRule, the browser tries to include this file: /product/ford-mustang/stylesheets/style.css. That file doesn't exists. Try to use absolute paths (like this: http://website.com/stylesheets/style.css) or make it relative to your domain (like this: /stylesheets/style.css, notice the leading slash).
@Ray I've used absolute path like example.com/css/template.css but the CSS file still won't work.
|
2

A URL rewrite for /products/ford-mustang would be:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.