Rewrite URL for PHP Containing Space and Dash

  Kiến thức lập trình

I need to update my website to have SEO friendly URLs. The website uses PHP and a MySQL database. The page in question takes a GET variable called cat (short for category) from the URL.

Here are a few examples of categories I need to work with. They can contain multiple spaces and dashes.

Hex Bolt

U-Bolt <- this category has a dash already

Tension Control Bolt

The current URL looks like this
example.com/catalog/parts/index.php?cat=Hex+Bolt

Ultimately I would like the URLs to look like the following:

example.com/catalog/parts/hex-bolt

example.com/catalog/parts/u-bolt <- leave the dash

example.com/catalog/parts/tension-control-bolt

I believe this involves an htaccess rule. How would you go about pulling this off where you could replace the dash back to a space vs keep the dash if it was initially a dash? If you could include the HTACCESS rule and any other handling needed on the PHP script(s). Thanks!

LEAVE A COMMENT