(基础)Mod_rewrite 细分
我花了几个小时查看关于 mod_rewriting 的各种评论,但我自己尝试的任何东西似乎都不起作用。我所希望的是,我可以为你们提供 URL 模型,并获得一个有效的规则和该规则的细分作为回报(我对正则表达式相当熟悉)。这样我也可以适应未来的要求。
结构是: domain.com/contents/page.php?id=123&u=page-slug
我想要的是用户输入: domain.com/page-slug
并成功重定向到相关页面。
据我所知,这不应该影响 PHP GET - 这是正确的吗?即使 id 实际上不在地址栏中(明显的问题,但我对此完全陌生)?令人惊讶的是教程遗漏了什么......
提前致谢!
I'm at few hours looking a various tuts on mod_rewriting, but anything I try myself never seems to work. What I'm hoping for is that I can give you guys the URL model, and get a working Rule and a breakdown of that Rule in return (I'm reasonably familiar with regex). That way I can adapt to future requirements also.
The structure is:domain.com/contents/page.php?id=123&u=page-slug
What I want is for the user to type:domain.com/page-slug
and be successfully redirected to the page in question.
From what I have read, this shouldn't affect PHP GET's - is this correct? Even if the id isn't actually in the address bar (obv. question, but I'm completely new to this)? It's surprising what tutorials leave out...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 ID 是必需的,那么它需要出现在漂亮的 url 中,例如
domain.com/page-slug/page-id
。要将其重写为
page.php
,请将以下内容添加到domain.com
根目录中的 .htaccess 中,如下所示。如果您想了解更多信息,请参阅此 .htaccess 和 mod_rewrite 列表网络资源
If the ID is necesary then it will need to be present in the pretty url i.e. something like
domain.com/page-slug/page-id
.To rewrite this to
page.php
add the following to your .htaccess in the root directory ofdomain.com
as belowIf you want to learn more see this list of .htaccess and mod_rewrite resources on the web
如果 url 中没有 id,PHP GET 将无法获取 id。但在你给出的代码中它会起作用。因为你在 htaccess 中设置了固定的 id,所以 id 将始终为 123。
为什么它似乎永远不起作用?给你一个错误,一个 apache 错误,或者......?
您是否已将 mod_rewrite 模块添加到 httpd.conf 中?如果没有:
浏览到您的 httpd.conf 文件并在编辑器中打开它。在文件中查找:
删除行前面的
#
,保存文件并重新启动 apache。您的 .htaccess 文件应如下所示:
然后将domain.com/id/page-slug 替换为domain.com/page.php?id=id&u=page-slug
If there is no id in the url, PHP GET won't get an id. But in the code you give it will work. Because you set a fixed id in the htaccess, the id will always be 123.
And why does it never seems to work? get you a error, a apache error, or...?
Have you add the mod_rewrite module to the httpd.conf? If not:
Browse to you httpd.conf file and open it in your editor. Look in the file for:
Remove the
#
in the front of the line, save the file and restart apache.Your .htaccess file should look like:
Then it replace domain.com/id/page-slug to domain.com/page.php?id=id&u=page-slug