我如何$_GET正确的ID

发布于 2024-10-07 20:35:19 字数 585 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

じее 2024-10-14 20:35:19

L 标志会导致将已重写的 URL 重新注入到重写过程中。因此,当 /42/picture/ 被重写为 /picture/?id=42 时,它就会被重写为 /index.php?page=picture >。

您可以通过在其他规则前面放置一个规则来避免这种情况,该规则会停止每个可以映射到现有文件的路径的请求的重写过程:

RewriteCond %{REQUEST_FILNAME} -f
RewriteRule ^ - [L]

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:

RewriteCond %{REQUEST_FILNAME} -f
RewriteRule ^ - [L]
ˇ宁静的妩媚 2024-10-14 20:35:19

var_dump 的输出来看,似乎第一个规则匹配,而不是第二个规则。您编写规则的顺序很重要,您可能应该恢复这两个规则。由于您的第一条规则较短。

RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]

您可以查看文档以供参考。

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.

RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]

You can take a look at the documentation for your reference.

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