php – URL重写包括标题

最初我成功使用id重写url

使用htaccess代码

RewriteRule ^link/([0-9]+)\.html$sub_index.PHP?link_id=$1 

链接代码

a href="link/id.html"> 

它成功显示

然后我试着像这样制作网址:
mysite.com/link/Trkking in Nepal / 6.html
“尼泊尔徒步旅行”是数据库中的标题

我写了htaccess代码

RewriteRule ^link/([a-zA-Z0-9_-]+)/([0-9]+)\.html$sub_index.PHP?link_id=$1 

链接代码

a href="link/title/id.html">

我无法成功地将我的网址重写为所需的格式并获取消息
在服务器上找不到请求的URL

此外,当我在其他网站搜索,我没有看到空间,标题写成“徒步在尼泊尔”.

我想知道,需要帮助

谢谢

解决方法:

首先,您的重写代码不正确:

RewriteRule ^link/([a-zA-Z0-9_-]+)/([0-9]+)\.html$sub_index.PHP?link_id=$1

你的link_id是2美元,你的标题是1美元.所以你应该使用:

RewriteRule ^link/([a-zA-Z0-9_-]+)/([0-9]+)\.html$sub_index.PHP?link_id=$2

然后你应该使用slug函数作为你的标题,所以它更友好的url.我使用这样的东西:

function slug($string, $spaceRepl = "-") {
  // Replace "&" char with "and"
  $string = str_replace("&", "and", $string);

  // Delete any chars but letters, numbers, spaces and _, -
  $string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);

  // Optional: Make the string lowercase
  $string = strtolower($string);

  // Optional: Delete double spaces
  $string = preg_replace("/[ ]+/", " ", $string);

  // Replace spaces with replacement
  $string = str_replace(" ", $spaceRepl, $string);

  return $string;
}

slug(“尼泊尔徒步旅行”)将在尼泊尔徒步旅行,所以您的链接将是:

/link/trekking-in-nepal/6.html

它应该与您的重写代码一起工作.

另外,我喜欢像这样重写我的链接

/link/trekking-in-nepal-6.html

为此,我使用以下内容

RewriteRule ^link/([a-zA-Z0-9_-]+)-([0-9]+)\.html$sub_index.PHP?link_id=$2

祝好运!

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...