[英]How to $_GET['id'] from a clean url like "example.com/products/123/title-of-this-product"?
I'd like my URL to look like this:我希望我的 URL 看起来像这样:
example.com/products/123/title-of-this-product
The actual URL is this:实际的 URL 是这样的:
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
文件正确解释了我的 URL,因此该页面上的用户只能看到干净的 URL。但是,如果我尝试在products.php
页面上使用$_GET['id']
,脚本会崩溃,因为它不会识别 URL 中的任何id
。
.htaccess
code: .htaccess
代码:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]
products.php
code: products.php
代码:
$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?如果我想要一个干净的 URL,如何保留 PHP 函数的 URL 参数?
You actually have 2 problems here...你实际上在这里有2个问题......
MultiViews
(part of mod_negotiation) is enabled and it's this that is serving products.php
. MultiViews
(mod_negotiation 的一部分)已启用,它正在为products.php
提供服务。RewriteRule
pattern is incorrect and won't match the requested URL.RewriteRule
模式不正确,与请求的 URL 不匹配。Options +FollowSymLinks +MultiViews
You need to disable MultiViews
(you have explicitly enabled it).您需要禁用
MultiViews
(您已明确启用它)。 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.为您的
products.php
文件(没有任何 URL 参数)提供服务的是 MultiViews(mod_negotiation 的一部分),而不是后面的 mod_rewrite 指令。 MultiViews essentially allows extensionless URLs with minimal effort, however, it can be the cause of unexpected conflicts (with mod_rewrite) - as in this case. MultiViews 本质上允许以最小的努力使用无扩展名的 URL,但是,它可能是意外冲突的原因(使用 mod_rewrite)——就像在这种情况下一样。
Your RewriteRule
directive is not actually doing anything.您的
RewriteRule
指令实际上没有做任何事情。 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).如果
.htaccess
文件位于文档根目录中,则RewriteRule
模式^([0-9]+)(?:/([^/]*))?/?$
与请求的 URL ( /products/123/title-of-this-product
),所以该指令实际上根本没有被处理(尽管 MultiViews 仍然会覆盖它,即使它被处理了)。
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 .您从与
RewriteRule
pattern匹配的 URL 路径的开头缺少products
。 Without products/
at the start of the regex it would only match if you are in a /products/
subdirectory, ie.如果没有
products/
在正则表达式的开头,它只会在您位于/products/
子目录中时匹配,即。 /products/.htaccess
. /products/.htaccess
。 The RewriteRule
directive matches relative to the location of the .htaccess
file itself. RewriteRule
指令与.htaccess
文件本身的位置相匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.