2016-08-13 44 views
0

我的原始網址是/localhost/site/en/detail.php?id=1 & title = Hello%20World 我想要一個乾淨的網址:/ localhost/site/EN /職位/ 1 /您好,世界,但問題是,對象未找到,重寫URL:找不到對象(.htaccess)

從HTML請求

<a href="post/'.$rij['id'].'/'.$rij['title'].'"><div class="material"><i class="fa fa-1x fa-chevron-right"></i></div></a> 

php文件和PHP處理程序:

$titlestring = mysqli_real_escape_string($db, $_GET['title']); 
$idstring = mysqli_real_escape_string($db, $_GET['id']); 

$query = "SELECT * FROM posts WHERE id=".$idstring; 

我將做BIND後來變量

這是我htaccessfile

RewriteEngine on 
RewriteBase/
#external redirect from actual URL to pretty one (remove query string) 
RewriteCond %{THE_REQUEST} \s/+detail\.php\?id=$1&?title=([^\s&]+) 
RewriteRule^%1? [R=302,L,NE] 
# convert all space (%20) to hyphen 
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [N,NE] 
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE] 
# rewrite rule to call actual PHP handler 
RewriteRule ^post/([0-9]+)/([-a-zA-Z_-]+) detail.php?id=$1&title=$2 [L,QSA,NC] 

回答

0

「對象未找到,」聽起來像是從正在處理該請求你的PHP代碼中的錯誤。您將標題參數中的空格替換爲連字符,因此如果您在請求處理程序中未對此進行說明,則可能會看到破損。

如果您的id是唯一的(預計他們應該是),那麼您可能會刪除任何引用殘缺的title的代碼。否則,您需要確保將標題轉換回舊結構以使用它。

沒有處理請求的代碼示例,我無法提供要更改的準確示例。但是,讓我們假設你正在做的事情一樣

$stmt = $mysqli->prepare("SELECT * FROM posts WHERE id=? and title=?"); 
$stmt->bind_param('is', $_GET['id'], $_GET['title']); 

你應該把它改爲只

$stmt = $mysqli->prepare("SELECT * FROM posts WHERE id=?"); 
$stmt->bind_param('i', $_GET['id']); 

或類似的東西:

$stmt = $mysqli->prepare("SELECT * FROM posts WHERE id=? and title=?"); 
$title = str_replace('-', ' ', $_GET['title']); 
$stmt->bind_param('is', $_GET['id'], $title); 
+0

我不認爲這是一個問題。我更新了我的代碼,並且我沒有在查詢中真正使用該標題, –