0

I am creating a small e-commerce website. There we have a list of items which user can click and explore the information about the item. I am passing the item id and and some other information in the query string so on the next page I can get the related data from database.

Right now we have the following URL default structure:

www.somedomain.com/item.php?id=1&some1=somevalue&some2=somevalue

But I want the URL like:

www.somedomain.com/item/1/somevalue/somevalue

Where item is item.php and /1/somevalue/somevalue is query string ?id=1&some1=somevalue&some2=somevalue. I want my whole website query string parameters like above.

I am using core PHP, no framework. I did some with .htaccess files but the server gives errors while loading the website. How can I do this with .htaccess file?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Stack Overflow
  • 1
  • 5
  • 23
  • 51

1 Answers1

2

Try with below, I am using mod_rewrite to achieve the result, clear cache if using any previous attempts.

Below will also cover '_' underscore and next rule after will be for only one item.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)/([a-zA-Z]+)/([a-zA-Z]+)$ $1.php?id=$2&some1=$3&some2=$4 [L]
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)$ $1.php?item=$2[L]
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45