我如何$_GET正确的ID
picture.php
if (!isset($_GET['id'])) {
header('location:/');
exit();
} else if (isset($_GET['id'])) {
print_r($_GET);
}
$_GET 输出
Array ( [page] => picture )
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]
URL 格式:http://local.com/42/picture/
问题:如何制作$_GET['id']
将打印 42
而不是 图片
picture.php
if (!isset($_GET['id'])) {
header('location:/');
exit();
} else if (isset($_GET['id'])) {
print_r($_GET);
}
$_GET output
Array ( [page] => picture )
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]
URL format : http://local.com/42/picture/
Question : How to make the $_GET['id']
will print 42
and not the picture
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
L 标志会导致将已重写的 URL 重新注入到重写过程中。因此,当
/42/picture/
被重写为/picture/?id=42
时,它就会被重写为/index.php?page=picture
>。您可以通过在其他规则前面放置一个规则来避免这种情况,该规则会停止每个可以映射到现有文件的路径的请求的重写过程:
The L flag causes a re-injection of the already rewritten URL into the rewriting process. So when
/42/picture/
gets rewritten to/picture/?id=42
it then gets rewritten to/index.php?page=picture
.You can avoid this by putting a rule in front of your other rules that stops the rewriting process for every request that’s path can be mapped onto an existing file:
从
var_dump
的输出来看,似乎第一个规则匹配,而不是第二个规则。您编写规则的顺序很重要,您可能应该恢复这两个规则。由于您的第一条规则较短。您可以查看文档以供参考。
From the output of your
var_dump
, it seems the first rule is matching and not the second. The order you write rules matters, you should probably revert the two rules. Since your first rule is shorter.You can take a look at the documentation for your reference.