How can I create a dynamic URL in php?

偶尔善良 提交于 2019-12-04 21:50:50

Its not quite clear what your asking. If you want to create a page that lists the urls of your books, then you are not far off with your echo statement. You just need to populate $bookid and $booktitle from the database. .htaccess is not involved.

echo "<a href='http://sitename.com/".$bookid."-".seo($booktitle)."'>".$booktitle."</a>";

But if you want to unpack the URL of the link the user clicked, then you need to look at the query string passed to the page. .htaccess breaks up the URL for you and passes the $1 parameter into your script. To read the url in PHP try the following

parse_str($_SERVER['QUERY_STRING'],$query);
if (array_key_exists('id',$query)) {
   $books = explode("-",$query['id']);
}

This will create an array with the book id in the first element ($books[0]), and the first word of the title in the second etc. (If you wanted to use this approach and have the whole title in the second you might want to use a different character to delimit the id from the title to the character you use to replace spaces.

Replace your rewrite rule to this:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^([^-]+)-(.+)$ /book.php?id=$1 [L,QSA]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!