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]
网址格式:http://local.com/42/picture/
问题:如何使$_GET['id']
打印 42
而不是picture
答案 0 :(得分:1)
从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]
您可以查看documentation以供参考。
答案 1 :(得分:1)
L 标志会将已重写的URL重新注入重写过程。因此,当/42/picture/
被重写为/picture/?id=42
时,它会被重写为/index.php?page=picture
。
您可以通过在其他规则之前放置一条规则来避免这种情况,该规则会阻止路径可以映射到现有文件的每个请求的重写过程:
RewriteCond %{REQUEST_FILNAME} -f
RewriteRule ^ - [L]