1

Motive: I am building my own simple cms. But when doing htaccess for my posts I am stuck when I want to add text after the id passing in url.

What i have tried so far is:

RewriteRule ^blog/(.+)$ post.php?id=$1

The problem is that when I type in http://example.com/blog/1 I get the post returned. but when I go to http://example.com/blog/1/hello-world its not working. I want the text hello-world added to permalinks for SEO purpose.

3 Answers 3

2

In addition to writing regex in .htaccess, you could also route all of URLs to one PHP script, like Wordpress does:

   <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . /index.php [L]
   </IfModule>

Then, inside index.php you can split $_SERVER['REQUEST_URI'] to parts and process as you wish, like this:

$parts = explode("/", $_SERVER['REQUEST_URI']);
if($parts[1]==="blog")
{
    $id = intval($parts[2]);
    include("post.php");
    die();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Another great solution but as question suggests I want it through htaccess rewrite.
But this is .htaccess rewrite ;p
@IdidntKnewIt - yes, I agree. I just noticed that you're writing your own CMS, so perhaps taking the routing logic into PHP would be more convenient, than configuring it in .htaccess all the time. Easy to forget to change, harder to install, etc.
I have built more than half of it added all post.php edit.php page.php and etc.php... so can't modify now.
@IdidntKnewIt You can incorporate the routing change to index.php only. Other scripts don't need changes.
1

Below RewriteRule working fine for me.

RewriteRule ^blog/([0-9]+)/(.*)?$ blog_detail.php?id=$1&title=$2 [L]

3 Comments

The title I want will be retrieved from data base and I don't need that as variable.
@IdidntKnewIt but that's the other way around ;) A rewrite rule parses the URI when it's coming in, getting the title from the database (your hello-world part in your example) is only relevant when you are creating the uri's (when you are constructing a link for instance)
One addition to this: you should consider to make the slug unique in your database and omit the id altogether; an url like /blog/1/hello-world is a three-level url, while /blog/hello-world is a two-level url. Search engines consider a two-level url more important. And as you are checking the id AND the slug anyway, you don't really need the id (you probably do need it internally though, so don't remove the column from your database)
0

Since your 'ugly' url does not use the title, you can simply match it, but ignore it. You are currently matching 1/hello-world in your first capture group, and your post.php page can obviously not handle this. Instead match the following:

RewriteRule ^blog/([^/]+)/[^/]+$ post.php?id=$1 [L]

Edit: Someone claims that this approach is inefficient. I beg to differ. You can do the following in php to redirect requests that were done using an incorrect url. It's exactly as efficient as a comparison of $_GET['title'] and $expectedtitle, then constructing the url. The difference is that you have no useless variables lying around, and the intention of this code is clear. You want to redirect the user if the url is not the expected url. That the expected title is not the actual title is a sideproduct.

$expectedtitle = getTitleById( $_GET['id'] );
$expectedurl = "{$_GET['id']}/{$expectedtitle}";
if( $_SERVER['REQUEST_URI'] != $expectedurl ) {
  header( $expectedurl, TRUE, 301 );
}

16 Comments

so what I need to modify is just adding a + before $
You'll have to be really careful with this, because /blog/1/hello-world and /blog/1/bye-world will both be matched and both return the same post, resulting in duplicate content... So you'll have to cross your fingers search engines will not find out about other possible url's
No i will add canonical tags to head adding post title with spaces str_replace'd with '-'.
@IdidntKnewIt No; if you add a + before the $ in your rule the capture group will still be greedy and gobble up all characters.
@giorgio That's something mod_rewrite has no part in. That's something blog.php should do based on %{REQUEST_URI} as far as I am concerned.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.