簡體   English   中英

.htaccess干凈的網址不起作用

[英].htaccess clean url doesn`t work

這是我的表格

<form id="search" action="search.php" method="GET">
        Search : 
        <input type="text" id="searchKeyword" name="searchKeyword" placeholder="Search Keyword" />
        <input type="submit" id="btnSubmit" name="submit" value="search" /> 
    </form>

這是我的search.php文件:

<?php 
include("includes/connection.php"); 

header('Content-type: text/html; charset=utf-8');
?>

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Search script by Mode2Code.Org</title>
    <link rel="stylesheet" type="text/css" href="style/style.css">
</head>
<body>
<a href="index.php" id="back">Back!</a>

    <?php
    $keyword = $_GET['searchKeyword'];

    $keyword = htmlspecialchars($keyword);

    $insertQuery = "INSERT INTO searched (keyword) VALUES ('" . $keyword . "')";

    $insert = mysql_query($insertQuery);
    ?>
</body>

當我鍵入關鍵字並按搜索按鈕時,它使網址如下所示:website.com/search.php?searchKeyword=sometext

但我希望我的網址像這樣websute.com/search/sometext

如果我的.htaccess

  Options +FollowSymLinks
  RewriteEngine On

  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteCond %{SCRIPT_FILENAME} !-f

  RewriteRule ^profile/(\w+)$ ./search.php?searchKeyword=$1
  RewriteRule ^profile/(\w+)/$ ./search.php?searchKeyword=$1

我認為.htaccess中沒有任何錯誤,但無法正常工作。 當我嘗試轉到此URL website.com/search/sometext時,它顯示404錯誤。

誰能幫我 ?

PS我是PHP的初學者:|

試試下面的代碼。

首先更改表單操作,因為默認操作是發布search.php,否則您將需要重定向它。

<form id="clean" method="get" action="./search/">
    <input type="search" name="searchKeyword" value="Text" />
    <input type="submit" name="submit" />
</form>

其次,要正確重定向,您需要添加一些Javascript代碼,如果不包含表單,則在表單所在的頁面中包含jQuery:

<script>
    $("#clean").submit(function(event) {
        event.preventDefault();
        $(location).attr('href',$(this).attr("action") + $("input[name$='searchKeyword']").val());
    });
</script>

最后,將您的.htaccess文件更改為一個更簡單的版本,您的重定向頁面也是個人資料。

RewriteEngine On
#RewriteBase /stackoverflow/  : You may need to define the base too if it's in subdirectory
RewriteRule ^search/(.*)/?$  search.php?searchKeyword=$1 [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM