如何重写这样的url?

发布于 2024-09-18 12:40:35 字数 273 浏览 1 评论 0原文

我将如何重写(对于我博客中的每一篇文章):

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/13

AND(没有 id )

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/

How would I rewrite ( for every posts in my blog ):

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/13

AND ( without id )

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

捎一片雪花 2024-09-25 12:40:35

试试这个规则:

RewriteEngine on
RewriteRule ^[^/]+/(\d+)$ post.php?id=$1

这会将请求的 URI 路径(如 /this-is-the-title-of-the-post/13)内部重写为 /post.php?id=13;所以这与你所描述的相反。但这就是 mod_rewrite 的工作原理:您只能更改传入请求,而不能更改传出响应。

现在,如果您想省略数字 ID,您的标题需要是新 ID:

RewriteRule ^this-is-the-title-of-the-post/(13)?$ post.php?id=13

这意味着它需要是唯一的。否则标题无法明确映射到数字 ID。在这种情况下,如果将标题传递给 post.php,标题肯定会更容易:

RewriteCond %{REQUEST_FILENAEM} !-f
RewriteRule ^([^/]+)/$ post.php?title=$1

显然,由于标题是唯一的,因此您不再需要数字 ID。

Try this rule:

RewriteEngine on
RewriteRule ^[^/]+/(\d+)$ post.php?id=$1

This will rewrite a requested URI path like /this-is-the-title-of-the-post/13 internally to /post.php?id=13; so it’s rather the other way round of what you described. But that’s how mod_rewrite works: You can only change incoming requests and not outgoing responses.

Now if you want to omit the numeric ID, your title needs to be the new ID:

RewriteRule ^this-is-the-title-of-the-post/(13)?$ post.php?id=13

That means it needs to be unique. Otherwise the title cannot be mapped onto the numeric ID unambiguously. In that case the title it would certainly be easier if you pass the title to post.php:

RewriteCond %{REQUEST_FILENAEM} !-f
RewriteRule ^([^/]+)/$ post.php?title=$1

Obviously, since the title is unique, you don’t need the numeric ID any more.

余罪 2024-09-25 12:40:35

mod_rewrite 是一个 Apache 模块,它允许您使用正则表达式来执行此操作。如果您使用 Windows/IIS Web 服务器,您将拥有完全不同的技术;我会坚持使用阿帕奇。

您的第一个示例非常简单,因为重写的 URL 只需要在正确的位置包含 ID 即可工作。前面的答案已经给出了可行的解决方案。

第二个示例(即重写的 URL 中没有 ID)需要修改底层 PHP 来查询数据库以查找页面标题而不是 ID(这可能会对您的网站产生性能影响,具体取决于您的方式)写的)。或者,您可以为每个单独的 URL 制定特定的重写规则,但这需要相当大的维护开销。

mod_rewrite 的坏消息是让它工作可能是一门黑暗的艺术,特别是如果你不擅长正则表达式。问题是,如果它不起作用,您只会在浏览器中收到服务器配置错误,并且调试起来会非常困难。如果您愿意坚持下去,这是值得的,但对于初学者来说可能会令人沮丧。

好消息是,如果您使用的是 WordPress 或 Drupal 等 CMS 产品,它们内置了 URL 重写功能,因此您根本不需要搞乱服务器配置。

希望有帮助。

mod_rewrite is an Apache module which allows you to do this using regular expressions. If you're using a Windows/IIS web server, you'll have entirely different techniques; I'll just stick with Apache.

Your first example is quite easy because the rewritten URL just needs to contain the ID in the correct place for it to work. The previous answer has already given a workable solution.

The second example (ie without the ID in the rewritten URL) would need to you modify your underlying PHP to query the DB to lookup the page title rather than the ID (This may have performance implications for your site, depending on how you've written it). Alternatively, you could have a specific rewrite rule for each individual URL, but that is quite an overhead to maintain.

The bad news with mod_rewrite is that it can be quite a dark art to get it working, expecially if you're not good with regular expressions. The trouble is that if it doesn't work you just get a server config error in your browser, and it can be very hard to debug. It is worth the effort if you're willing to persevere, but it can be frustrating for a beginner.

The good news is that if you're using a CMS product such as Wordpress or Drupal, these have URL rewrite functionality built in, so you don't need to mess around with server config at all.

Hope that helps.

赢得她心 2024-09-25 12:40:35

如果您只想拥有纯文本 URL(URL 中没有 pk 引用),则必须在数据库中实现唯一的 SLUG 字段(对于日期来说可以是唯一的,就像 wordpress 那样)。
我建议重定向所有与现有文件不匹配的请求到index.php并处理$_SERVER['REQUEST_URI']并避免处理mod_rewrite magick(从长远来看会导致无法维护和magick代码)。 mod_rewrite 规则

If you want to have just text only URLs (without pk reference in URL), you must implement unique SLUG field in database (which can be unique for date, like wordpress does).
I suggest redirecting all requests, which does not match existing files to index.php and deal with $_SERVER['REQUEST_URI'] and avoid handling mod_rewrite magick (with in long term leads to unmaintanable and magick code). mod_rewrite rule for this

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