1

I was looking on google and couldn't find anything useful.

Basically, I need to make an html/php-based marketplace but I'm stuck on 1 thing. I want to have a custom link like domain.com/store/tshirt.

What I mean is that I want to generate a custom page with a custom link like wordpress has, without making new html/php pages for each product, because it would take a lot of space and time to do it.

So basically, what I'm looking for is a way to generate custom pages with custom links using data from the database, which is inserted from admin-panel like wordpress.

2 Answers 2

3

What you're talking about is using rewrite rules in an .htaccess file.

It takes a few steps to do.

  1. Create an .htaccess file if you don't already have one.

  2. Create your rewrite rule

  3. Change the links on your page to match the new rewrite rule.

  4. Change your sql query

Example:

You have a link to a page like this... example.com/products.php?id=24

You want your URL to look like this... example.com/belgian-coffee

Step 1) make your rewrite rule in .htaccess like this...

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule    ^([A-Za-z0-9_-]+)$    products.php?id=$1    [NC,L]    # Handle page requests`

Step 2) change the link from <a href="/products.php?id=24">Belgian Coffee</a> to <a href="/<?php echo str_replace(' ', '-', $row['ProductName']) ?>">Belgian Coffee</a>
Note: The str_replace function will replace any spaces in your product name to hyphens

Step 3) Before you query the database for the product name "reverse engineer" your passed parameter. In this case... belgian-coffee like this...

$productname = str_replace('-', ' ', $_GET['id'])

Step 4) Change your sql statement to query for the product name instead of the product ID

All done!

Reference

htaccess manual
htaccess guide

Sign up to request clarification or add additional context in comments.

1 Comment

awesome solution, finally I could do it.
0

for this you could follow a MVC architecture in php so that u could use a custom link for custom pages to learn mvc YOU can follow this tutorial else you could use .HTACCESS

Comments

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.