PHP+.htaccess: How to create URL like with ID+name of an article?

允我心安 提交于 2019-12-08 07:45:18

问题


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

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

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]

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>

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?


回答1:


This my my .htaccess file :

RewriteEngine on
RewriteBase /

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

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

In my code I use this method :

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

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



来源:https://stackoverflow.com/questions/14663256/php-htaccess-how-to-create-url-like-with-idname-of-an-article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!