(基础)Mod_rewrite 细分

发布于 2024-12-27 06:07:14 字数 377 浏览 2 评论 0原文

我花了几个小时查看关于 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 技术交流群。

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

发布评论

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

评论(2

孤独患者 2025-01-03 06:07:14

如果 ID 是必需的,那么它需要出现在漂亮的 url 中,例如 domain.com/page-slug/page-id

要将其重写为 page.php,请将以下内容添加到 domain.com 根目录中的 .htaccess 中,如下所示。

RewriteEngine on
RewriteBase /

#if not a file or a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite requests of the form domain.com/page-slug/123
RewriteRule ^([-a-zA-Z0-9]+)/([0-9]+)/?$ contents/page.php?id=$2&u=$1 [NC,L,QSA]

如果您想了解更多信息,请参阅此 .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 of domain.com as below

RewriteEngine on
RewriteBase /

#if not a file or a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite requests of the form domain.com/page-slug/123
RewriteRule ^([-a-zA-Z0-9]+)/([0-9]+)/?$ contents/page.php?id=$2&u=$1 [NC,L,QSA]

If you want to learn more see this list of .htaccess and mod_rewrite resources on the web

七婞 2025-01-03 06:07:14

据我所知,这不应该影响 PHP GET - 这是正确的吗?即使 id 实际上不在地址栏中(明显的问题,但我对此完全陌生)?

如果 url 中没有 id,PHP GET 将无法获取 id。但在你给出的代码中它会起作用。因为你在 htaccess 中设置了固定的 id,所以 id 将始终为 123。


为什么它似乎永远不起作用?给你一个错误,一个 apache 错误,或者......?
您是否已将 mod_rewrite 模块添加到 httpd.conf 中?如果没有:

浏览到您的 httpd.conf 文件并在编辑器中打开它。在文件中查找:

#LoadModule rewrite_module modules/mod_rewrite.so

删除行前面的#,保存文件并重新启动 apache。


您的 .htaccess 文件应如下所示:

RewriteEngine On
RewriteBase /

RewriteRule ^(\d*?)/(.*?)$ page.php?id=$1&u=$2

然后将domain.com/id/page-slug 替换为domain.com/page.php?id=id&u=page-slug

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)?

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:

#LoadModule rewrite_module modules/mod_rewrite.so

Remove the # in the front of the line, save the file and restart apache.


Your .htaccess file should look like:

RewriteEngine On
RewriteBase /

RewriteRule ^(\d*?)/(.*?)$ page.php?id=$1&u=$2

Then it replace domain.com/id/page-slug to domain.com/page.php?id=id&u=page-slug

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