duanci8209
duanci8209
2015-06-11 18:06

找不到.htaccess重写规则页面

已采纳

If page name doesn't exist then I would like to show page not found instead of a blank white page.

.htaccess:

RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ page.php?name=$1 [L,QSA,NC]

page.php:

$name = filter_input(INPUT_GET, 'name');

$result = $mysqli->query("SELECT * FROM pages WHERE name='$name'");

while ($row = $result->fetch_assoc()) {
    echo $row['content'];

}

If some of you know a better way, please share it :)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douyi1084 douyi1084 6年前

    You can change your PHP code like this:

    $name = filter_input(INPUT_GET, 'name');
    
    if ($result = $mysqli->query("SELECT * FROM pages WHERE name='$name'")) {
       if ( $result->num_rows == 0 ) {
          printf('404 / page Not Found');
          exit;
       }
       else {
          while ($row = $result->fetch_assoc()) {
             echo $row['content'];
          }
       }
    } else {
       printf("Error: %s
    ", $mysqli->error);
       exit;
    }
    
    点赞 评论 复制链接分享
  • dongzhuange2625 dongzhuange2625 6年前

    You can get the mysql num rows and make if it == 0 then show page not found before the while loop

    点赞 评论 复制链接分享

相关推荐