Skip to content
Advertisement

Hide URL parameter from URL to get page info in PHP

I have a website to show products and my URLs are like this
www.website-name.com/product/product-name

I used to use this URL to get the ID of the product
www.website-name.com/product/product-id/product-name
but I don’t want the product ID to be displayed in the URL, if I remove it I will not be able to make GET request in PHP to get the product info from database.

Note: I know I can use the product name parameter to get the info from database but I am not suggesting that because maybe sometimes I will have two products with same name.

Is there a way to hide the the product-id parameter from URL, or any other suggestions to approch this?

Advertisement

Answer

You can´t get id from URL if you don´t pass as a GET parameter.

I suggest interpreting the name of the product and from there identify the ID. But if there are duplicates, the only way is to pass the ID.

One trick is to pass the Id as part of the product name. For example

www.website-name.com/product/**product-name-product-id.html**

In this way, the product name would be part of the URL. This is solved by changing the htaccess like this

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.+)/.*-([0-9]+).html$ index.php?me=$1&pr=$2 [L]

where me is menu “product” and pr is product id. I hope it would be useful for you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement