简体   繁体   English

使用.htaccess文件向网址添加$ title

[英]Add $title to url with .htaccess file

I recently had the following answered: .htaccess rewrite url does not work 我最近有以下答案: .htaccess重写url不起作用

I would like to now add the title from the db for each id to the URL too. 我现在也想将数据库中每个ID的标题也添加到URL中。 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. 通常,我在每个php文件中都有$title ,它引用数据库中的标题。

Currently I'm using this in my current .htaccess file: 目前,我正在当前的.htaccess文件中使用此文件:

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> . 以及以下内容为href: <a href=\\"cars/$id\\"/>$title</a>

So what I wish to have as my URL is the following. 因此,我希望以下是我的URL。 Basically I need to add the title to the current URL and replace the spaces between words in title with dashes. 基本上,我需要将标题添加到当前URL,并用短划线替换标题中单词之间的空格。

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

  • cars -> subfolder 汽车->子文件夹
  • 543 -> $id (From the database) 543-> $ id(来自数据库)
  • new-porsche-is-out -> $title (From the database) new-porsche-is-out-> $ title(来自数据库)

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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