如何从像“example.com/products/123/title-of-this-product”这样的干净网址 $_GET['id'] ?

发布于 2025-01-17 03:41:00 字数 777 浏览 2 评论 0原文

我希望我的 URL 看起来像这样:

example.com/products/123/title-of-this-product

实际的 URL 是这样的:

example.com/products.php?id=123&title=title-of-this-product

.htaccess 文件正确解释我的 URL,以便此页面上的用户只能看到干净的 URL。但是,如果我尝试在 products.php 页面上使用 $_GET['id'],脚本会崩溃,因为它无法识别任何 id< URL 中的 /code>。

.htaccess 代码:

Options +FollowSymLinks +MultiViews

RewriteEngine On
RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]

products.php 代码:

$product_id = isset($_GET['id']) ? $_GET['id'] : "Error! I can't find your product!";

如果我想要一个干净的 URL,如何保留 PHP 函数的 URL 参数?

I'd like my URL to look like this:

example.com/products/123/title-of-this-product

The actual URL is this:

example.com/products.php?id=123&title=title-of-this-product

The .htaccess file correctly interprets my URLs so that users who are on this page only see the clean URL. However, if I try to use $_GET['id'] on the products.php page, the script crashes because it doesn't recognize any id in the URL.

.htaccess code:

Options +FollowSymLinks +MultiViews

RewriteEngine On
RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]

products.php code:

$product_id = isset($_GET['id']) ? $_GET['id'] : "Error! I can't find your product!";

How can I retain the URL parameters for PHP functions if I want a clean URL?

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

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

发布评论

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

评论(1

金兰素衣 2025-01-24 03:41:00

实际上,您在这里有两个问题...

  1. MultiViews(mod_negotiation 的一部分)已启用,并且它正在为 products.php 提供服务。
  2. 您的 RewriteRule 模式不正确,与请求的 URL 不匹配。
选项 +FollowSymLinks +MultiViews

您需要禁用MultiViews(您已明确启用它)。为您的 products.php 文件(不带任何 URL 参数)提供服务的是 MultiViews(mod_negotiation 的一部分),而不是后面的 mod_rewrite 指令。 MultiViews 本质上允许以最小的努力实现无扩展 URL,但是,它可能会导致意外冲突(使用 mod_rewrite) - 正如本例所示。

您的 RewriteRule 指令实际上并未执行任何操作。如果 .htaccess 文件位于文档根目录中,则 RewriteRule 模式 ^([0-9]+)(? :/([^/]*))?/?$ 与请求的 URL (/products/123/title-of-this-product) 不匹配,因此指令为根本没有实际处理(尽管 MultiViews 仍然会即使确实如此,也要覆盖它)。

请尝试这样操作:

# Disable MultiViews
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteRule ^products/([0-9]+)(?:/([^/]*))?/?$ products.php?id=$1&title=$2 [L,NC]

您在与 RewriteRule 模式 匹配的 URL 路径开头缺少 products。如果正则表达式开头没有 products/ ,则仅当您位于 /products/ 子目录中时才会匹配,即。 /products/.htaccessRewriteRule 指令相对于 .htaccess 文件本身的位置进行匹配。

You actually have 2 problems here...

  1. MultiViews (part of mod_negotiation) is enabled and it's this that is serving products.php.
  2. Your RewriteRule pattern is incorrect and won't match the requested URL.
Options +FollowSymLinks +MultiViews

You need to disable MultiViews (you have explicitly enabled it). It is MultiViews (part of mod_negotiation) that is serving your products.php file (without any URL parameters), not the mod_rewrite directive that follows. MultiViews essentially allows extensionless URLs with minimal effort, however, it can be the cause of unexpected conflicts (with mod_rewrite) - as in this case.

Your RewriteRule directive is not actually doing anything. If the .htaccess file is located in the document root then the RewriteRule pattern ^([0-9]+)(?:/([^/]*))?/?$ does not match the requested URL (/products/123/title-of-this-product), so the directive is not actually processed at all (although MultiViews would still override this even if it did).

Try it like this instead:

# Disable MultiViews
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteRule ^products/([0-9]+)(?:/([^/]*))?/?$ products.php?id=$1&title=$2 [L,NC]

You were missing products from the start of the URL-path matched by the RewriteRule pattern. Without products/ at the start of the regex it would only match if you are in a /products/ subdirectory, ie. /products/.htaccess. The RewriteRule directive matches relative to the location of the .htaccess file itself.

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