Htaccess - 未找到404的Modrewrite验证

时间:2013-10-17 04:28:32

标签: php .htaccess

如何找不到404的Comprobe?

这是我的代码php:

$id = $_GET['id'];
$slug = "my-slugified-title-post-in-this-example"; // Slug is dinamic according to id

echo "This post is here!";

这是我的htaccess

RewriteRule ^posts/([0-9]+)/(.*?).html post.php?id=$1$slug=$2 [L,NC]

如果url中的$ slug错误,我想要404错误:

http://mysite.com/posts/274/my-slugified-title-post-wrong-in-this-example.html //slug is wrong

这是好的!

http://mysite.com/posts/274/my-slugified-title-post-in-this-example.html

1 个答案:

答案 0 :(得分:1)

如果slug是错误的,你必须将它与从id中拉出的slug进行比较。所以在你的PHP脚本中,你会有类似的东西:

$id = $_GET['id'];
$slug = "my-slugified-title-post-in-this-example"; // Slug is dinamic according to id

if ( $slug != $_GET['slug'] ) {   // slug is different
    header('HTTP/1.0 404 Not Found');
    exit();
}

echo "This post is here!";