不确定如何让 mod_rewrite 写入某种格式
我试图了解如何正确使用 mod_rewrite。我的数据库中有带有标题和日期的帖子。现在我正在使用 mysite.com/post.php?id=3 来检索我的帖子数据并填充页面。我希望这样:mysite.com/2011/03/27/my-title-like-so/。我猜我必须在数据库中查询除 id 之外的其他内容,但我不知道。有人可以帮忙吗?也许我忽略了一些非常简单的事情。到目前为止,我的显示如下:mysite.com/post/2/,但这并没有多大帮助:P
谢谢!
I'm trying to understand how to properly use mod_rewrite. I have posts in my database that have a title and date. Right now I'm using mysite.com/post.php?id=3 to retrieve my post data and populate the page. I would like to have it like this: mysite.com/2011/03/27/my-title-like-so/. I'm guessing I'll have to query my database for something other than the id, but i dont know. Can anyone help? Perhaps I'm over looking something very simple. So far I have it showing up like this: mysite.com/post/2/ , but that doesn't help very much :P
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
常见做法是在 URL 中包含 ID 和标题/日期。这样对于搜索引擎来说看起来不错,并且您仍然可以通过 ID 高效地检索它。例如,查看此 stackoverflow 问题的 URL。
只是猜测,但 URL 中的数字可能是生成的 ID。因此,您使用 ID 并忽略 URL 的其余部分,例如:
A common practice is to include both the ID and the title/date in the URL. That way it looks nice to search engines and you can still retrieve it efficiently by ID. As an example, look at the URL for this stackoverflow question.
Just a guess but that number in the URL is probably a generated ID. So you use the ID and just ignore the rest of the URL, e.g.:
如果您不希望在 URL 中包含 ID,那么我建议采用以下方法。
在“帖子”表中,包含一个“url”(或“友好”或其他内容)字段,其中包含您希望访问帖子的 url。插入帖子时,只需删除所有非单词字符并用“-”替换它们即可。
然后,当 URL 进入您的系统时,您可以直接将此 url 映射到“url”字段。
因此,使用您的示例,您的 URL 将是 http://mysite。 com/2011/03/27/my-title-like-so/
“url”字段将包含“my-title-like-so”。
您可以使用简单的重写规则,如下所示:
这会将 URL 的内容放在域之后:
并将其存储在 $_GET 全局中作为“myvar”
然后您可以在 php 中对该 url 进行任何处理:
现在你有一个包含 URL 所有部分的数组,你可以用它们做任何你想做的事情。
If you do not wish to include the ID in the URL, then I would suggest the following approach.
In your "posts" table, include a "url" (or "friendly" or something) field that contains the url you wish your post to be accessed by. When you insert the post, simply strip all non-word characters and replace them with a '-'.
Then, when the URL comes into your system, you can just map this url directly to the "url" field.
So, using your example, your URL would be http://mysite.com/2011/03/27/my-title-like-so/
The "url" field would contain 'my-title-like-so'.
You could use a simple rewrite rule such as the following:
This would put the contents of the URL after the domain:
And would store it in the $_GET global as 'myvar'
Then you can do any processing on that url in php:
Now you have an array of all the parts of the URL and you can do whatever you wish with them.
您必须捕获传递给引擎的值以提取所需的数据。所以:
应该符合你的格式。
然后
http://mysite.com/fetch.php
可以使用$_GET
的值来获取数据库并显示好的东西。You must capture the values that are passed to the engine to extract the data you want. So:
should match your format.
Then the
http://mysite.com/fetch.php
can use the$_GET
s values to fetch the database and display the good things.