PHP +的.htaccess:如何创建URL像ID的文章+名字?名字、文章、htaccess、PHP

2023-09-02 20:34:17 作者:卟忘初心、方得始終

我在想如何创建这样的路由系统:

I am thinking how to create the routing system like this:

http://website.com/something/12345-my-new-car

它说,在URL被放置的物品的ID和蛞蝓名称。 我目前的设置如下( http://website.com/something/12345 ):

which says that in URL is placed the ID and slug name of an article. My current setup is the following (http://website.com/something/12345):

RewriteRule ^something/(.*)$  index.php?id=something&something_id=$1 [L,QSA]

现在我想补充的蛞蝓名称后面的文章ID。 第一个想法 - 手工打造它 - 是这样的:

Now I would like to add the slug name behind the article ID. First idea - manually build it - something like:

<a href="/something/<?php echo $id.'-'.$slug;?>">link</a>

然后再获取当前网址和爆炸()解析URL( / ),然后爆炸()解析 - 在 12345-MY-新车 =>这回我 12345 。现在我只想挑选出相应的物品。

And then to get the current URL and with explode() parse the url (/) and then again explode() for parsing - in the 12345-my-new-car => which return me 12345. And now I would just pick out the respective article.

但我真的不喜欢这种方式,这是一个有点笨拙。

But I don't really like this way, it's a bit clumsy.

有没有什么更好的方法来实现这一目标?

Is there any better way to achieve this goal?

推荐答案

这是我的我的的.htaccess 文件:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]

在我的code我用这个方法:

In my code I use this method :

$agri = explode("/",$_GET['args']);

和我有表,我argumants。现在我可以做这一切。

And I have table with my argumants. Now I can make with this all.