简体   繁体   中英

Add $title to url with .htaccess file

I recently had the following answered: .htaccess rewrite url does not work

I would like to now add the title from the db for each id to the URL too. From what I've read on the internet it seems that it is possible. Mostly I have $title in each php file which refers to the title in the db.

Currently I'm using this in my current .htaccess file:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1 [R=302,L]

# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+)/?$ /cars/cars.php?cars_item=$1 [NC,L]

And the following as href: <a href=\\"cars/$id\\"/>$title</a> .

So what I wish to have as my URL is the following. Basically I need to add the title to the current URL and replace the spaces between words in title with dashes.

http://www.domain.com/cars/543/new-porsche-is-out

  • cars -> subfolder
  • 543 -> $id (From the database)
  • new-porsche-is-out -> $title (From the database)

You can just change your last rule to allow title in the end:

# Internally forward cars/231/title to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM