不确定如何让 mod_rewrite 写入某种格式

发布于 2024-10-27 05:30:31 字数 253 浏览 1 评论 0原文

我试图了解如何正确使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-11-03 05:30:31

常见做法是在 URL 中包含 ID 和标题/日期。这样对于搜索引擎来说看起来不错,并且您仍然可以通过 ID 高效地检索它。例如,查看此 stackoverflow 问题的 URL。

http://stackoverflow.com/questions/5451267/not-sure-how-to-get-mod-rewrite-to-write-to-a-certain-format

只是猜测,但 URL 中的数字可能是生成的 ID。因此,您使用 ID 并忽略 URL 的其余部分,例如:

RewriteRule /questions/(\d+) question.php?id=$1

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.

http://stackoverflow.com/questions/5451267/not-sure-how-to-get-mod-rewrite-to-write-to-a-certain-format

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.:

RewriteRule /questions/(\d+) question.php?id=$1
清风疏影 2024-11-03 05:30:31

如果您不希望在 URL 中包含 ID,那么我建议采用以下方法。

在“帖子”表中,包含一个“url”(或“友好”或其他内容)字段,其中包含您希望访问帖子的 url。插入帖子时,只需删除所有非单词字符并用“-”替换它们即可。

然后,当 URL 进入您的系统时,您可以直接将此 url 映射到“url”字段。

因此,使用您的示例,您的 URL 将是 http://mysite。 com/2011/03/27/my-title-like-so/
“url”字段将包含“my-title-like-so”。

您可以使用简单的重写规则,如下所示:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # if requesting file that doesn't exist, use the rule below
RewriteCond %{REQUEST_FILENAME} !-d # if requesting a directory that doesn't exist, use the rule below
RewriteRule ^(.*)$ index.php?myvar=$1 [L,QSA]

这会将 URL 的内容放在域之后:

http://mysite.com/[catches everything here]

并将其存储在 $_GET 全局中作为“myvar”

然后您可以在 php 中对该 url 进行任何处理:

$url_string = my_sanitising_code($_GET['myvar']);
$url_array = explode('/', $url_string);

现在你有一个包含 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:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # if requesting file that doesn't exist, use the rule below
RewriteCond %{REQUEST_FILENAME} !-d # if requesting a directory that doesn't exist, use the rule below
RewriteRule ^(.*)$ index.php?myvar=$1 [L,QSA]

This would put the contents of the URL after the domain:

http://mysite.com/[catches everything here]

And would store it in the $_GET global as 'myvar'

Then you can do any processing on that url in php:

$url_string = my_sanitising_code($_GET['myvar']);
$url_array = explode('/', $url_string);

Now you have an array of all the parts of the URL and you can do whatever you wish with them.

[浮城] 2024-11-03 05:30:31

您必须捕获传递给引擎的值以提取所需的数据。所以:

RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ fetch.php?year=$1&month=$2&day=$3&title=$4 [L]

应该符合你的格式。
然后http://mysite.com/fetch.php可以使用$_GET的值来获取数据库并显示好的东西。

You must capture the values that are passed to the engine to extract the data you want. So:

RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ fetch.php?year=$1&month=$2&day=$3&title=$4 [L]

should match your format.
Then the http://mysite.com/fetch.php can use the $_GETs values to fetch the database and display the good things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文