Eric
Eric

Reputation: 43

Rewrite query into path using htaccess

How can I make htaccess rewrite the following URL structure:

http://example.com/detail.php?id=polo&categ=wears

to

http://example.com/wears/polo/

I have tried the below code but it didn't work for me.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images|img|products|slide)/(.*)$ $2/$3 [L]
RewriteRule ^(.*)/(.*)/$ detail.php?id=$1&categ=$2
</IfModule>

Upvotes: 1

Views: 47

Answers (2)

Zoltan Tolnai
Zoltan Tolnai

Reputation: 208

Your code works for me when I include the trailing slash in the URL.

working -> http://example.com/wears/polo/

not working -> http://example.com/wears/polo

Also, you need to exclude the slash character from the .* expression.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images|img|products|slide)/(.*)$ $2/$3 [L]
RewriteRule ^([^/]*)/([^/]*)/?$ detail.php?id=$1&categ=$2
</IfModule>

Upvotes: 1

Peace
Peace

Reputation: 626

Try this

RewriteEngine on
RewriteRule ^/(\w+)/(\w+)$ detail.php?categ=$1&id=$2 [NC,L]

Upvotes: 0

Related Questions