简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM