问题
Thanks in advance for any help.
I am trying to figure out how to rewrite dynamic urls created with a simple cms programming I am building. The code outputs the URL with the ID (www.mysite.com/index.php?id=1). I would like to modify the code so that it outputs the title of the row in the db with the id of 1 such as www.mysite.com/home or www.mysite.com/about where about is the title stored in the row of the db with an id of 1.
Here is an example of how the code outputs:
$result = mysql_query("SELECT id, title FROM pages");
while ($row = mysql_fetch_array($result)) {
// Do not list the home page twice
if ($row['id'] != $homeID) {
$pageID = $row['id'];
$pageTitle = $row['title'];
echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID'>$pageTitle</a></li>";
}
I am going off of the CMS tutorial from: http://fwebde.com/web-design/creating-a-php-cms-part-1/
Any help would be greatly appreciated. I have attempted numerous .htaccess rewrites and NONE of them have worked. Please let me know if this is as simple as switching around the MySQL code.
回答1:
You will have to modify the line that generates the links then:
echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID'>$pageTitle</a></li>";
You can either add the title as separate and unused &title=
parameter, which is a nice hack to avoid having to modify anything else:
echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID&title=$pageTitle'>$pageTitle</a></li>";
Or generate 123+title
links:
echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID+$pageTitle'>$pageTitle</a></li>";
This would require to adapt the actual "cms" dispatcher code, because it has to break up the id and the title from the $_GET parameter.
Or even replace the whole index.php?id=
with just the title:
echo "<li><a href='" . BASE_URL . "/$pageTitle'>$pageTitle</a></li>";
Which in turn would require to deploy a .htaccess
rule (something like htaccess URL routing for PHP MVC?). And you would have to modify the index.php
script again to look up the id
from title
. Which again would only work if the titles in your database were all unique.
P.S.: You should actually also use urlencode($pageTitle)
. And htmlspecialchars()
on the $pageTitle
that becomes the link text.
回答2:
It wont be as easy as you would like. The simplest way would be to include the page number in your final url. http://www.mysite.com/1/articles
but I don't know if that would be exactly what you are looking for.
The rewrite rule would be.
RewriteRule ^([0-9]*)/.*$ index.php?id=$1
What I have done on my own CMS systems is to add another field to the pages table called urlname
. This would be what the URL would be to access that page. You would need to change all the references to $_GET['id'].
Add this to functions.php.
function thisPageId(){
$sql="SELECT id FROM pages WHERE urlname = '" . $_GET['urlname'] ."'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
return $row['id'];
}
htaccess rewrite rule
This will redirect any request that does not exist to index.php?urlname=...
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) index.php?urlname=$1
Replace any references to $_GET['id'] with the value from thisPageId. For your listPages function and anywhere else that the url is displayed, you'll do something like this.
echo "<li><a href='" . BASE_URL . "/" . $row['urlname'] . "'>$pageTitle</a></li>";
Remember you'll also have to edit your administration pages to add the new field.
来源:https://stackoverflow.com/questions/7789567/rewrite-id-to-page-title-stored-in-db-row