I am very new to PHP and MySQL and I have some questions.
First, I have a menu like this: home.php, about.php, etc. When someone access home.php?page=1&opt=2&etc, I want the URL to appear domain.com/home/1/2/etc . Is there any way to achieve this?
Second, it is a good practice to save the pages content in a database and then display it from that database when the page is requested? I mean if the link is home.php?page=1&opt=2&etc, I have to do a MySQL database interrogation with those parameters, and if it is found I have to display the field (which contains html/php code). It is correct? Or what approach should I have?
Thanks!
As Col. Shrapnel said, it’s the other way around, but you should do something else.
This article may help you.
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
Or, assuming you are using Apache, you could do something like this:
In your .htaccess:
This will return the requested resource if it exists or transform your URL:
http://www.domain.com/1/2
into
http://www.domain.com/index.php?args=/1/2
Somewhere in your PHP:
You will have to parse the one parameter: $_GET[“args”]
Which from the example it would contain “/1/2”
Note that all requests are going to enter through index.php.
I would suggest you to design your URL’s carefully and write a parser as general as possible. For instance:
Will /x/y always mean: page=x opt=y or sometimes it will mean for example invoice=x line=y?
Maybe you could consider to add some more info to the request:
http://www.domain.com/pages/1/2 Would clearly refer to pages.
Of course that, if you only will dispatch pages, the first option will work ok.