如何更改 GET 变量在 php 中的工作方式

发布于 2024-08-23 13:45:51 字数 88 浏览 2 评论 0原文

我不喜欢这样的内容:example.com/?id=2222。如何将其更改为example.com/2222

I dont like something like this: example.com/?id=2222. How to change it into example.com/2222?

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

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

发布评论

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

评论(3

喵星人汪星人 2024-08-30 13:45:51

您需要查看 mod_rewrite

来自文档:该模块使用基于规则的重写引擎(基于正则表达式解析器)来动态重写请求的 URL。

因此,对于用户来说,URL 可能看起来像 example.com/2222,但随后在服务器上转换为 example.com/?id=2222。

You need to look into mod_rewrite.

From the documentation: This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly.

So the url can look like example.com/2222 for the user, but then translated into example.com/?id=2222 on the server.

[旋木] 2024-08-30 13:45:51

确保启用了 mod_rewrite,并且您需要在 .htaccess 文件中添加几行内容,类似于:

RewriteEngine on
RewriteBase /
RewriteRule (.*)$ index.php?id=$1 [L]

您可以获得更多信息(带有大量示例)此处,或在搜索引擎中搜索“htaccess Pretty urls”。

Make sure mod_rewrite is enabled, and you'll need a few lines in an .htaccess file, similar to:

RewriteEngine on
RewriteBase /
RewriteRule (.*)$ index.php?id=$1 [L]

You can get more information (with lots of examples) here, or by searching for "htaccess pretty urls" in a search engine.

故事与诗 2024-08-30 13:45:51

如果您无法使用 mod_rewrite (这是最好的方法),您可以使用 404 陷阱。

也就是说,通常 example.com/2222 会向用户显示 404 页面。如果您将服务器设置为指向您选择的脚本而不是默认的 404 页面,那么您现在可以使用 URL example.com/2222 并将用户重定向到您想要的任何位置。

If you are unable to use mod_rewrite (which is the best way), you could use a 404 trap.

That is, normally example.com/2222 would show a 404 page to the user. Well if you set your server to point to a script of your choice instead of the default 404 page, you can now take the URL example.com/2222 and redirect the user to wherever you want.

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