如何更改 GET 变量在 php 中的工作方式
我不喜欢这样的内容: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要查看 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.
确保启用了
mod_rewrite
,并且您需要在 .htaccess 文件中添加几行内容,类似于:您可以获得更多信息(带有大量示例)此处,或在搜索引擎中搜索“htaccess Pretty urls”。
Make sure
mod_rewrite
is enabled, and you'll need a few lines in an .htaccess file, similar to:You can get more information (with lots of examples) here, or by searching for "htaccess pretty urls" in a search engine.
如果您无法使用 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.