Reputation: 459
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^-([0-9]+) /simpleblog/index.php?post_id=$1
This is how the link in formatted.
<a href = "index.php?post_id=<?php echo $post['posts_id']; ?>-<?php echo $post['title']; ?>"><?php echo $post['title'];?></a>
Right now the link is like:
http://localhost/simpleblog/index.php?post_id=15-First%20Post
I want the link to be like:
http://localhost/simpleblog/post/First-Post
I new with working with the .htaccess. I have no idea how to use it right. So how do I make the link the way I want it to be like?
Upvotes: 0
Views: 738
Reputation: 42984
Things won't work the way you a trying to implement:
The url you want to see in web browsers does not state the numerical id. It certainly is possible to rewrite that url to whatever you want on a syntactical level. But there is no way for the rewriting module to guess that a post with that title has the numeric id 15. That cannot be picked from thin air.
So Either you have to accept some numerical id as part of the urls you publish, or you have to implement a routine that is able to reference a post by it's alphanumerical title. However considering that post titles might have all sorts of special chars and the like inside such alphanumerical reference is error prone. This is why most sites use a combination of both: numerical id AND alphanumerical title. The title is silently dropped by the rewriting module whilst the numerical id is what is used internally to reference the post.
Upvotes: 1
Reputation: 11809
According to your comment to my question about parameter 15
in your examples, my conclusion is post/First-Post
URL segment really holds 3 different parameters, called par1
to par3
in this answer.
If that is correct, you may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/([^-]+)-([^/]+)/? [NC]
RewriteRule .* simpleblog/index.php?post_id=%1-%2_%3 [L]
Maps silently
http://localhost/simpleblog/par1/par2-par3
(This is the URL entered in browser's bar)
To
http://localhost/simpleblog/index.php?post_id=par1-par2_par3
In your question there is a space (%20) between par2 and par3, that is replaced in the redirected query with _
to avoid problems.
If you definitely need the space, make the conversion in index.php
with a code like this one:
<?php
if (isset ($_GET['post_id']) ) {
$PostId = $_GET['post_id'] ;
$PostId = str_replace('_',' ',$PostId);
echo $PostId . "<br /><br />"; // Test
}
?>
Upvotes: 1
Reputation: 22243
You are trying to create SEF (Search Engine Friendly) urls, i think that the best way is to create a table on your db:
sef_urls(original_url,sef_url)
where you store the original url (index.php?post_id=15-First%20Post) and the sef url you want (post/First-Post), then in the .htaccess:
RewriteRule ^(.*)$ index.php [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in your index.php you query your db table to know what is the original url. For example, i write:
localhost/simpleblog/post/First-Post
index.php does:
$requested_url="..";
$result=mysql_query("SELECT original_url FROM sef_urls WHERE sef_url=$requested_url");
if($row=mysql_fetch_array($result)){//is this SEF url a known url?
include($row['original_url'];
}else{
//404 not found
}
Upvotes: 0