Php: .htaccess "smooth Urls" 2008-08-09 12:26:59
If you've been on Newgrounds for any length of time, you've probably noticed that the BBS URLS contain bbs/topic/1. You may be thinking, "Isn't having a directory for each object overkill?" It's not. They don't use it. Instead, they use .htaccess mod_rewrite and PHP to create sexy URLS, and you can too!
OK, now. Create a .htaccess file in the root dir of your site (/) containing:
RewriteEngine on RewriteRule ^stuff/([0-9]+)/?$ /view.php?type=stuff&id=$1
Explanation: This code first turns the RewriteEngine on. Then, it redirects all requests for stuff/8558 or any number to view.php?type=stuff&id=8558 in your root dir. This works by first saying ^, which indicates the start of a pattern. Then, we say stuff/, which makes it only work in the nonexistant dir "stuff". Now, we enclose the regex [0-9]+ with () to make it so we can access it using $1. Now, we type /?, which tells it to redirect to view.php regardless if it has a ending slash or not. Next, $ signifies the end of the pattern. Now, we tell it to redirect all requests for stuff/# to view.php in the root dir, passing GET variables type and id. After id, note the $1. That means to steal whatever the user passes in [0-9]+ and assign it to id. The next step is to actually create view.php in your root dir, and make it so it can retreive data from a database and display it based the type and id number passed. Example I use on my site:
<? INITIALIZE DATABASE $data = mysql_query("SELECT * FROM content WHERE id=$_GET[id ]"); $info = mysql_fetch_array($data); ?> <?=$info[data]?>
If you has question, just ask.