2

I would like to use following URL for my web site.

http://mywebsite.com/product.php
to
http://mywebsite.com/product

http://mywebsite.com/product.php?id=5
to
http://mywebsite.com/product/5


http://mywebsite.com/product.php?action=delete&id=5
to
http://mywebsite.com/product/delete/5



http://mywebsite.com/product.php?action=edit&id=3
to
http://mywebsite.com/product/edit/3

I use this code on my .htaccess file

RewriteEngine on

RewriteRule ^product product.php

RewriteRule ^product/([a-zA-Z0-9_-]+)$ product.php?id=$1
RewriteRule ^product/([a-zA-Z0-9_-]+)/$ product.php?id=$1

But problem is when i use http://mywebsite.com/product/5 and display with GET variable, it does not pass the id parameter and how can i achieve the multiple query from my url?

1 Answer 1

1

Try this htaccess :

RewriteEngine on

RewriteRule ^product/?$ product.php [L]

RewriteRule ^product/([a-zA-Z0-9_-]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/delete/([a-zA-Z0-9_-]+)/?$ product.php?action=delete&id=$1 [NC,L]
RewriteRule ^product/edit/([a-zA-Z0-9_-]+)/?$ product.php?action=edit&id=$1 [NC,L]
Sign up to request clarification or add additional context in comments.

4 Comments

can you tell me what is the difference between two lines? RewriteRule ^product/?$ product.php [L] and RewriteRule ^product product.php
Ashis , ^product matches a product.php or productss any character after t because there is no end of line. using $** restricts it to match **product** and a optional slash only. **$ is important here to prevent rewrite loop error.
Now got it. Thanks for clarification
[L] is a rewrite flag of apache mod_rewrite, it means Last (it tells apache to stop processing other rules if the request has matched the currunt rule)

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.