Rewrite URL: Object not found (.htaccess)


Rewrite URL: Object not found (.htaccess)

我原来的url是/localhost/site/en/detail.php?id=1&title=Hello%20World我将一个干净的url:/localhost/site/en/posts/1/Hello-World,但问题是,对象没有找到

来自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]

"Object is not found"听起来像是处理请求的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);