URL重写,包括标题 [英] URL Rewriting including title

查看:88
本文介绍了URL重写,包括标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我成功使用id重写了网址



带有htaccess代码

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

和链接代码

  a href = link / id.html> 

显示成功:



然后我尝试使网址如下:
mysite.com/link/Trekking in Nepal / 6.html
其中 Trekking in Nepal是数据库中的标题



我写了 htaccess代码

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

和链接代码

  a href = link / title / id.html> 

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



另外,当我在其他网站上搜索时,我没有看到空格,标题的写法是尼泊尔徒步旅行。 / p>

我想知道,需要帮助



谢谢

解决方案

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

  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 

然后您应该使用标题的子函数,因此对URL更友好。我用这样的东西:

  function slug($ string,$ spaceRepl =-){
//替换为&带有和
$ string = str_replace(&, and,$ string);的char

//删除任何字符,但字母,数字,空格和_,-
$ string = preg_replace( / [^ a-zA-Z0-9 _-] /, ,$ string);

//可选:使字符串小写
$ string = strtolower($ string);

//可选:删除双精度空格
$ string = preg_replace( / [] + /,,$ string);

//用替换
替换空格$ string = str_replace(,$ spaceRepl,$ string);

返回$ string;
}

sl(尼泊尔徒步旅行)将是尼泊尔徒步旅行,因此您的链接将是:

  /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 

祝您好运!

Initially i succeed to rewrite the url using id

with the htaccess code:

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

and link code

a href="link/id.html"> 

It displays successfully :

Then i tried to make url like this: mysite.com/link/Trekking in Nepal/6.html where "Trekking in Nepal" is a title in database

I wrote htaccess code:

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

and link code:

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

I cannot successfully rewrite my url to the desired format and get the message The requested url is not found on the server

Also when i searched in other sites, i didn't see space, the title is written like "Trekking-in-Nepal".

I am wondering, need help

Thanks

解决方案

First, your rewrite code is not correct:

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

Your link_id is $2, your title is $1. So you should use:

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

Then you should use a slug function for your title, so it's more url friendly. I use something like this:

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("Trekking in Nepal") will be trekking-in-nepal, so your link will be:

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

It should work then with your rewrite code.

Also, I like to rewrite my links like this:

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

For this I use the following:

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

Good luck!

这篇关于URL重写,包括标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆