简体   繁体   English

重写URL:找不到对象(.htaccess)

[英]Rewrite URL: Object not found (.htaccess)

my original url was /localhost/site/en/detail.php?id=1&title=Hello%20World and i would a clean url: /localhost/site/en/posts/1/Hello-World but the problem is that the object is not found 我原来的网址是/localhost/site/en/detail.php?id=1&title=Hello%20World,我会使用一个干净的网址:/ localhost / site / en / posts / 1 / Hello-World,但问题是该对象找不到

the request from html 来自html的请求

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

the php file and the php handler: 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;

I will do the BIND variables later 稍后我将做BIND变量

And this is my htaccessfile 这是我的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" sounds like an error from your PHP code that is handling the request. “找不到对象”听起来像是来自处理请求的PHP代码中的错误。 You are replacing spaces in your title parameter to hyphens, so if you're not accounting for this in your request handler, you will probably see breakage. 您正在将title参数中的空格替换为连字符,因此,如果您不考虑请求处理程序中的空格,则可能会看到损坏。

If your id s are unique (would expect they should be), you can probably excise any code that's referencing the mutilated title . 如果您的id是唯一的(希望它们应该是唯一的),则您可以删除引用残缺title任何代码。 Otherwise, you need to make sure you're transforming the title back to the old structure to use it. 否则,您需要确保将标题转换回旧结构以使用它。

Without an example of the code handling the request, I cannot provide an accurate example of what to change. 没有处理请求的代码示例,我无法提供更改内容的准确示例。 But, let's pretend you're doing something like 但是,让我们假装您正在做类似的事情

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

You should change it to either just 您应该将其更改为

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

or something like: 或类似的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM