如何从像“example.com/products/123/title-of-this-product”这样的干净网址 $_GET['id'] ?
我希望我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您在这里有两个问题...
MultiViews
(mod_negotiation 的一部分)已启用,并且它正在为products.php
提供服务。RewriteRule
模式不正确,与请求的 URL 不匹配。您需要禁用
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 仍然会即使确实如此,也要覆盖它)。请尝试这样操作:
您在与
RewriteRule
模式 匹配的 URL 路径开头缺少products
。如果正则表达式开头没有products/
,则仅当您位于/products/
子目录中时才会匹配,即。/products/.htaccess
。RewriteRule
指令相对于.htaccess
文件本身的位置进行匹配。You actually have 2 problems here...
MultiViews
(part of mod_negotiation) is enabled and it's this that is servingproducts.php
.RewriteRule
pattern is incorrect and won't match the requested URL.You need to disable
MultiViews
(you have explicitly enabled it). It is MultiViews (part of mod_negotiation) that is serving yourproducts.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 theRewriteRule
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:
You were missing
products
from the start of the URL-path matched by theRewriteRule
pattern. Withoutproducts/
at the start of the regex it would only match if you are in a/products/
subdirectory, ie./products/.htaccess
. TheRewriteRule
directive matches relative to the location of the.htaccess
file itself.