views:

418

answers:

5

I'm creating a blog engine as a learning exercise and one particular problem has me stumped. I'm wondering how are blog posts created in say a blog engine such as Wordpress? I'm thinking there are 2 ways you can do this:

1) Creating a new blog post called 'testPost' creates a new HTML page called www.myblog.com/testPost.html. So for each new blog post you save a new HTML page to the server. This method seems inefficient. A blog can have hundreds of blog posts, which means you would have to create hundreds of HTML pages. I don't think I want to use this method.

2) You have a generic blog post page whose data is rendered according to the blog post you are trying to access. For example if I created 'testPostOne' the generic blog post page would be filled with testPostOne's data and URL, if I created 'testPostTwo' then the generic page would render testPostTwo's respective contents and so on.

But using this method brings its own problems. For example how would you link to a page that doesn't actually exist? Linking to http://www.myblog.com/testPostOne.html would not work.

These are the two ways I could come up with to solve this problem. Maybe there is another way of doing this, I'm not sure. Please feel free to recommend a better way of solving this problem if you know of one.

Basically, I want to be able to have a nicely formatted url for each blog post without having to create a new HTML page on the server for each one.

EDIT: I might add that I'm using ASP.NET to do this so any methods available via this framework would be helpful

A: 

You'll need to make a dynamic page in, say, PHP that reads data from a database for the post content. If you want pretty URLs with your page, then you'll want to look into something like mod_rewrite for rewriting the URLs.

Ben Alpert
+1  A: 

Here is an open source blog engine written in ASP.NET 2.0 and one written in PHP (there are many others out there). You best bet is to check out the design and architecture and dissect how it (or something like it) does it job.

JP Alioto
+3  A: 

The basic idea would be to use a database. Each posting would be an entry in the DB, and you simply retrieve the data depending on the URL. For example,

www.myblog.com/posts.php?postid=1 or www.myblog.com/posts.aspx?postid=1

You can then either use URL rewrite methods to retrieve the same post with a cleaner URL, or better yet a RESTful method to do the same task.

Sev
If you like titles you could always use the Stack Overflow method and make the titles a URL.
Collin Price
A: 

If you dynamically create the page (as in suggestion 2), the http://www.myblog.com/testPostOne.html will exist when you try to access it, even though it is not an actuall file on the disk...

So suggestion 2 is probably the best way to go.

Arjan Einbu
Would it actually exist? I would have thought "testPostOne.html" would have to be the name of the identifier in the DB for a particular post. But it's more than likely going be a ID that you use so the URL would be "http://www.myblog.com?post_id=1"Unless you start using mod_rewite which is a whole new ball game.
gargantaun
@gargantaun: Does the myblog.com?post_id=1 exist? Does index.html exist? What defines the existense of a resource on a webserver?
Arjan Einbu
A: 

Personally, I use the Apache mod_rewrite. So when you have an URL like:

http://myblog.com/archives/my_very_first_post,

You can make a rewrite rule like this:

RewriteEngine on
RewriteRule ^archives/(.*)$ myblog.php?post=$1

Apache interprets "my_very_first_post" as a post ID and feeds it to a PHP script that handles the ID. The script then Fetches the post from database and displays it.

I believe this is the most common approach.

rogeriopvl