.htaccess修复漂亮的URL

时间:2018-08-03 21:43:28

标签: php .htaccess

我要删除.php文件扩展名并添加斜杠吗?
这是我的网址http://localhost/search.php?query=movie
我想要这样的http://localhost/search?query=movie
这是我的代码
htaccess

RewriteEngine On
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^search/(.*)$ ./search.php?query=$1
RewriteRule ^search/([a-zA-Z0-9]+)/$ search.php?query=$1



php

<?php
    require("classes/database.php");
    if (isset($_GET['query'])) {
      $query = trim($_GET['query']);
      if (strlen($query) >= 3) {
        try{
        $sql = "SELECT * FROM post WHERE title LIKE :query";
        $stmt = $conn->prepare($sql);
        $stmt->bindValue(':query','%'.$query.'%');
        $stmt->execute();
        while ($r=$stmt->fetch(PDO::FETCH_ASSOC)) {
          $name = DB::query('SELECT username FROM users WHERE id=:user_id', array(':user_id' => $r['user_id']))[0]['username'];
          if($r !== ''){
            ?>
            <a target="_blank" href="view.php?title=<?php echo $r['title']; ?>&id=<?php echo $r['id']; ?>&sidu=<?php echo $r['user_id']; ?>"><?php echo $r['title']; ?></a><br>
            <?php
          }else {
            echo "Not Found!";
          }
      }
      }catch(PDOException $e){
        echo $sql . "<br>" . $e->getMessage();
    }
      }
    }



    $conn = null;
 ?>

这是我的html

<form class="mx-2 my-auto d-inline w-100" action="search" method="get">
        <div class="input-group">
            <input type="text" name="query" class="form-control border border-right-0" placeholder="Search...">
            <span class="input-group-append">
            <button class="btn btn-outline-secondary border border-left-0" type="submit">
                <i class="fa fa-search"></i>
            </button>
          </span>
        </div>
    </form>

谢谢。任何帮助将不胜感激

对不起,我英语不好。

1 个答案:

答案 0 :(得分:1)

将此添加到标题中

<base href="/" />

,并且在.htaccess中

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([^/.]+)/([^/.]+)/([^/.]+)$ view.php?title=$1&id=$2&sidu=$3 [NC]