URL Rewriting in php .htaccess


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

URL Rewriting: “index.php?id=5” to “home.html”


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] 


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.




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



// 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 "
", print_r($page, true), "
";
}


POSSIBLE OUTPUT
Array (
  [id]    => 5
  [name]  => 'home.html'
)










Post a Comment

0 Comments