Change dynamically URL to SEO friendly via .htaccess?

EXAMPLE 1 – E-COMMERCE SITE

Sad Original URL = site.com/page.php?category=2&product=54

Happy URL 🙂 = site.com/sandwiches/rueben-sandwich/

step 1:

Make sure that all category names and product names are unique in your database.

step 2:

Replace all references to Original URL with the New URL throughout your website.

step 3:

Use mod_rewrite in your .htaccess file to parse out the elements of the URL. Like this:

RewriteEngine On

RewriteRule /(.*)/(.*)/$ page.php?category=$1&product=$2

the (.*) pulls the elements out and puts them in variables $1 and $2.

step 4:

Update your code on the page.php file to get the data from the database via the category and product name instead of by ID (this is why the names must be unique). For example:

Before:

"select * from database_table where categegory_id='$category' and product_id='$product'"

After:

"select * from database_table where categegory_name="$category" and product_name="$product""

NOTE: This is just a quick and simple example that doesn’t consider sanitizing input for security (which you must do) or any table joins you might need to do on your site.