-1

I have a shop page that contains all products from the database.

Upon clicking on any product it will take the user to the "product" page along with the product name through the URL using ?/xox_product_name

so the href of each product on the shop page goes like product?/xox_product_name

And on the product page, I use the below code to extract the product title

$end = array_slice(explode('/', $url), -1)[0];

Which works fine. But my client told me to lose the ? which is the issue.

On removing the "?" the href gonna be like 'product/xox_product_name' which actually searches for a folder name 'xox_product_name' and returns to 404 page.

How do I make the href go to the product page along with the product name without the ? in the URL

Any help is appreciated.

Harry
  • 81
  • 1
  • 10
  • are you using some PHP framework? do you have access to .htaccess file of the server? – Giacomo M Jun 01 '22 at 07:34
  • No, i m not using any framework. and yes, i have access to .htaccess file – Harry Jun 01 '22 at 07:35
  • you can use .htaccess to serve `product/product_name` url as it was `product?/product_name` – Giacomo M Jun 01 '22 at 07:36
  • can you elaborate a bit on how do i do that please – Harry Jun 01 '22 at 07:38
  • Most modern project redirect everything after the domain to a single file - usually index.php - there you can get the URL in it's full form and route accordingly. Here is an answer on how to do that: https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-using-htaccess – vuryss Jun 01 '22 at 07:38

1 Answers1

1

If you have access to .htaccess file, you can do it with url rewriting.

With this code:

RewriteRule ^product/([a-z0-9\-]+)$ product?/$1 [L]

This rule accept letters, numbers and the - character.
Make sure you have RewriteEngine on on the file either (before this line).

UPDATE

According to your comment, the rule you need is:

RewriteRule ^blog-detail/([\w\-]+)$ blog-detail?/$1 [L]

The meta-character \w matches any letter, number or underscore.

Giacomo M
  • 4,450
  • 7
  • 28
  • 57
  • It didn't work. my page is `'blog-detail'` and i m passing url as `'blog-detail/Types_of_Christian_Bridal_Gowns_in_2022'`. I added the mentioned line `RewriteRule ^blog-detail/([a-z0-9\-]+)$ blog-detail?/$1 [L]` right below the rewriteengine in .htaccess. it still give me the empty page with loading gif, which is a 404 default page in my hosting. – Harry Jun 01 '22 at 07:55
  • It still didn't work. My Full URL : `https://bhamaemporio.com/blog-detail/Types_of_Christian_Bridal_Gowns_in_2022` My .htaccess page : https://ibb.co/GVMKSpd Am i doing anything wrong..? – Harry Jun 01 '22 at 08:16