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 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. I’m not sure whether there are other options. 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
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,
http://www.myblog.com/posts.php?postid=1
or
http://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.
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.
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.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.
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:
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.