views:

351

answers:

3

Hi everybody,

I'm developping a website using php and the "template.inc" class. The problem is that I want to create a mini-cms that allows the admin to create an "html" page with these mysql attributes:

Table Name : Page
-----------------
id   :auto-icremented) 
name :varchar

In the architecture, if he created the page number "5", the url is

"ww.mywebsite.com/index.php?id=5".

But, this isn't very esthectic so, as I'm very bad at url rewriting even if i read many tutorials, i want to type the name+"html" to access to the page.

If we take the example of the

"www.mywebsite.com/index.php?id=5"

if the admin created a page with the following values:

id   : 5
name : 'home'

i want that the user can type

"www.mywebsite.com/home.html" 

and with no redirection as i want that this last url must still appear and become the official url.

Thanks for your answer, i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home'). How can i access to my database with the url rewriting engine?

Thank you very much, regards.

+1  A: 

Use Apache URL Rewriting for this. there are many many examples on this site alone of this. You could also try the official Apache rewriting docs.

You will need to make sure your database enforces uniqueness on name, or you will have problems.

Edit

Have your index.php take a name= parameter instead of a id parameter. You will need to make sure your db has the name field indexed so you don't do a table scan for every page request.

Byron Whitlock
Thanks for your answer,i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home').How can i access to my database with the url rewriting engine?
Zakaria
+4  A: 

Use the .htaccess file from a standard Wordpress install to redirect everything to one PHP file. Something like this...

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Base is the URL path of the home directory
    RewriteBase /
    RewriteRule ^$ /index.php [L]

    # Skip real files and directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /index.php [L]
</IfModule>

Then use $_SERVER['REQUEST_URI'] to figure out what the user was looking for. Modify the .* if you want to make the rule more specific, like .*\.html.

Renesis
Thanks for your answer,i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home').How can i access to my database with the url rewriting engine?
Zakaria
You are worrying about the ID too soon. URL rewriting cannot access your database. PHP will need to do this. Therefore, you just rewrite everything, and let PHP take the REQUEST_URI ("home.html") pull out the name ("home") then look it up in the database. The name will need to be unique.
Renesis
Thanks again! Very clear and userful answer!
Zakaria
+3  A: 

Zakaria,

After using Renesis's rewrite rule, $_SERVER['REQUEST_URI'] will be equal to 'home.html'.

Try something like:

<?php

// clean
$page = mysql_real_escape_string($_SERVER['REQUEST_URI']);

// make query
$query = sprintf("select Page.* from Page where name = '%s'", $page);

// find page ID
if($result = mysql_query($query)){

  $page = mysql_fetch_assoc($result);

  echo "<pre>", print_r($page, true), "</pre>";
}

?>

Possible output

Array (
  [id]    => 5
  [name]  => 'home.html'
)
macek
After writing this, I see that Zakaria ended up accepting an answer. Doesn't hurt to have more PHP help here, I guess :)
macek
+1 for taking the answer further
Renesis
Thanks, Renesis! You provided a great starting point. I learned how to skip real files/directories with your answer, too :)
macek
+1 for your answer! That's exactly what I did!Thanks again ;)
Zakaria