简体   繁体   中英

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

the request from 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:

$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

And this is my 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. 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.

If your id s are unique (would expect they should be), you can probably excise any code that's referencing the mutilated 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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