Create URLs from Database
I am trying to create clean URLs from information entered by the user. I have this in my .htaccess file.
RewriteRule ^[^/]+/$ /records.php [QSA,L]
This works great for when going to an address like so: example.com/blah/. What I have it doing is printing all records of category "blah" or whatever else the user enters (or I link to). The problem comes in when a user enters and stores a category with a space. I want to keep the integ开发者_开发技巧rity of the category with the space, but I also need to create a URL friendly version of the category. I'm linking to the categories like so:
$nospace = str_replace(" ", "-", $row['category']) ;
echo "<a href=/".$nospace."/>" . $nospace . "</a>";
The problem comes in when you arrive at the destination URL. If the category has a space, but I'm linking to a category with a -, I naturally don't get anything from that category. It doesn't exist.
How can I keep the space in the category name, but at the same time be able to create a URL friendly category name and link to it?
php (which is what it looks like you are using) supports urlencode
and urldecode
You can build your links with url encoded categories, and then on the other end url decode them. =)
echo '<a href="/'.urlencode($row['category']).'">'.$row['category']."</a>";
精彩评论