问题
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