2

我正在编写一个脚本,该脚本将从 URL 中选择一个术语,并根据该术语从我的数据库中提取正确的数据。

到目前为止,我已经在我的 .htaccess 文件中添加了一行,它将我的 URL 重写为 SEO 友好的格式,例如:

http://mydomain.com/catalogue/some-item-name

在实际的 PHP 页面上,我抓取该变量,去掉破折号

$item= str_replace('-', ' ', $_GET['item']);

并将该项目与我页面上的隐藏列表匹配以使用 jQuery 拉匹配。

一切正常,除非我在项目名称中有一个破折号。我的脚本删除了所有破折号。我可以吗:

  1. 把实际的破折号换成别的东西?
  2. 想办法只去掉一个破折号?

如果我从 db 中提取术语,我可以使用WHERE item LIKE "% $item %"但是因为我必须匹配我的 HTML 代码中已经存在的确切术语,所以我有点卡住了。

谢谢。

4

1 回答 1

1

We use a system where we check the incoming URI against the database and see where to send the user. This way, we can completely detach the URI from the system internals (such as IDs).

We match the incoming URI against the uri field in the database and load the correct function from the correct class (controller) or redirects. It also has the option to send parameters to the function.

Let me show by example

+---------+---------+----------+----------+--------+
| uri     | class   | function | new_uri  | vars   |
+---------+---------+----------+----------+--------+
| faq     | support | faq      |          |        |
+---------+---------+----------+----------+--------+
| sandwich| content | news     |          | 1      |
+---------+---------+----------+----------+--------+
| banana  |         |          | sandwich |        |
+---------+---------+----------+----------+--------+

Case 1: The user requests test.com/faq, the URI router finds a match on uri = 'faq' and tells the system to diplay whatever faq() in the support class or controller displays.

Case 2: The user requests test.com/sandwich which is a shortcut for a news story with the ID 1. So the URI router sends a call to news() in the content class and we can retrieve the id 1 from a variable like $_GET['vars'][0].

Case 3: The user requests test.com/banana, which has been moved to test.com/sandwich since we are not in the banana industry anymore. So the URI router politely sends a 301 and redirects the user to the correct place.

This method has many ways to expand, e.g. by allowing wildcards in the uri field etc. Hope it can be of some value to you too.

于 2013-10-28T12:57:45.177 回答