views:

128

answers:

3

Hi, I'm trying to make a clean url for a blog on a dynamic website, but I think that the problem is that I don't know how to plan the website schema.

I read about how to use mod_rewrite and all I found is how to make "http://www.website.com/?category&date&post-title" to "http://www.website.com/category/date/post-title". that's works o.k for me.

The problem is that If my url looks like "http://www.website.com/blog/?id=34" this method won't work as far as I got it.

So, I have two questions: 1. Is there a way to use mod_rewrite (maybe read from a txt file) to read the post title of my blog and rewrite my url by date and post-title? 2. Should I rewrite my website to query the data from one index file in the homepage and use mod_rewrite to write the nice url? should I query also the date and the title of the post instead just the post ID?

A: 

RewriteMap allows you to do all sorts of dynamic rewriting (text file, script, etc).

Ignacio Vazquez-Abrams
+2  A: 
  1. mod_rewrite used to rewrite requests and it has nothing to do with urls. You have to change urls by hands.
  2. yes, it's most common practice, to query the data from one index file
    no, you can't use mod_rewrite to write the nice url
    yes, an id must be present in the url along with title. your engine will just throw title away and use only id to retrieve an article.

Take a look at SO urls for an example

Col. Shrapnel
Thanks 2# It make sense... and I don't use mod_rewrite to write the nice url, I'm writing the nice url and the mod_rewrite redirect the page behind the scenes.
Sophia Gavish
+1  A: 

What you're talking about is commonly referred to as routing and lots of examples exist of different ways to do it with PHP. The most common approach uses the frontcontroller pattern, which means in the simple case rewriting all URLs to a single php file and then having that file determine what content to show dynamically based on the URL.

The most popular PHP frameworks (CakePHP, Symphony, Codeigniter, etc.) all have routing code in them which you might be able to use or might serve as inspiration. Alternatively this article covers lots of the basics if you want to do it yourself: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

Garethr