How do I $_GET correct ID
picture.php
if (!isset($_GET['id'])) {
header('location:/');
开发者_StackOverflow中文版 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
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]
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.
精彩评论