URL重写查询数据库? [英] URL Rewrite query database?
问题描述
我试图了解URL重写的工作方式.我有以下链接.
I'm trying to understand how URL rewriting works. I have the following link.
mysite.com/profile.php?id=23
我想用用户的名字和姓氏重写上述网址.
I want to rewrite the above url with the users first and last name.
mysite.com/directory/liam-gallagher
不过,从我所阅读的内容中,您可以指定应将url输出为的规则,但是如何查询我的表以获取每个用户名?
From what I've read however you specify the rule for what the url should be output as, but how do I query my table to get each user name?
抱歉,这很难理解,我很困惑!
Sorry if this is hard to understand, I've confused myself!
推荐答案
您是从错误的方向看的.您无法执行这种自动的URL重写.最好是创建一个全面的URL重写:
You are looking at this from the wrong direction. You can't do that kind of automatic url rewrite. The best is to create an all over url rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
,并在数据库中为用户创建一个特定名称,以用作url.
and create a specific name for a user in the db that will be used as an url.
+---------+----------+------+-----------+----------------+
| user_id | username | name | surname | url |
+---------+----------+------+-----------+----------------+
| 23 | liam | Liam | Gallagher | liam-gallagher |
+---------+----------+------+-----------+----------------+
现在,当有人访问您的http://mysite.com/directory/liam-gallagher
时,您可以阅读最后一个条目并在数据库中找到user_id
,然后让脚本完成其余的工作.
Now when someone accesses your http://mysite.com/directory/liam-gallagher
, you can read the last entry and find the user_id
in you database and make your script do the rest.
另一种方式是按照 Pekka 的建议.创建一个类似于http://mysite.com/directory/23/liam-gallagher
的网址,并从链接中读取ID.但是我个人不喜欢这种网址.我认为它们只是快速/懒惰的解决方法.
The other way is as Pekka suggested. Create an url like http://mysite.com/directory/23/liam-gallagher
and read the id from the link. But I personally don't like that kind of urls. They are just fast/lazy workarounds in my opinion.
这篇关于URL重写查询数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!