Reputation: 320
I'm not sure if this is a stupid question or not but I am creating an e-comm website at the moment which I would like to make as professional and secure as possible.
I want each product to have its own unique url for accessibility purposes but I'm not sure how to do this as right now I am using a GET variable to create unique pages dynamically.
It looks like this:
example.com/product?product=1
As you can see, this isn't a very accessible way to separate products (Using unique ID's) and it's also not very safe in terms of using with a database.
What I am wanting the unique url's to be like:
example.com/products/unique-product-name
I want to be able to use url's like this while still keeping the php simple for database use.
How can I do this quickly and simply without having to create individual pages for each product??
Upvotes: 0
Views: 98
Reputation: 17
I already worked on for that type of many project.
Please add in your .htaccess
file
RewriteEngine on
RewriteRule ^products/([A-Za-z0-9-]+)/?$ products.php?alias=$1 [NC,L]
and used anchor tag on like this
<a href"products/<?php echo $variable['alias']; ?>">product name</a>
Upvotes: 0
Reputation: 3577
First of all, you need to have a unique alias field for each product in your DB, so that you can access your product via GET
method like this:
www.example.com/product.php?alias=YOUR-ALIAS
And then you can use mod_rewrite to map www.example.com/products/YOUR-ALIAS
to the above link.
In order to do this, you have to create a text file called .htaccess
(NOTE: there is no file extension, the fullname of the file is .htaccess
) with the following rules in it:
RewriteEngine on
RewriteRule ^products/(.+)$ /product.php?alias=$1 [L]
You then have to upload the .htaccess
and product.php
files to the root of your server.
Upvotes: 4
Reputation: 320
I fixed the issue and got the same instance working on all 3 of my testing servers. For 2 of them to work I simply had to take the leading forward slash out of chiwangc's answer:
RewriteEngine on
RewriteRule ^products/(.+)$ product.php?alias=$1 [L]
I'm not 100% sure why this is the case only some of the time.
Upvotes: 1