[snipplet] Htaccess, HTML, PHP mod_rewrite directories to homepage

By.

min read

My profile

Share this:

Rewrite ALL urls [b:5ce86f39af]except[/b:5ce86f39af] /admin to index.php?p=directory

[b:5ce86f39af]3 examples:[/b:5ce86f39af]
www.site.com/test >> www.site.com/index.php?p=test
www.site.com/anothertest >> www.site.com/index.php?p=anothertest
www.site.com/admin >> [no rewrite]

[b:5ce86f39af]The htaccess part:[/b:5ce86f39af]
[code:1:5ce86f39af]RewriteEngine On

RewriteCond %{REQUEST_URI} !^admin*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L][/code:1:5ce86f39af]

[b:5ce86f39af]The HTML part:[/b:5ce86f39af]
Add a base href element in your head like:
[code:1:5ce86f39af]<base href=”http://www.site.com/” />[/code:1:5ce86f39af]

[b:5ce86f39af]The PHP part:[/b:5ce86f39af]
[code:1:5ce86f39af]// Mod rewrite
// Ramon Fincken, http://www.ramonfincken.com/permalink/topic56.html
if(isset($_GET[’p’]))
{
// Try..
$page = trim($_GET[’p’]);

// $page = str_replace(’_’,’ ‘,$page); // Optional
// Connect with your DB here ..
$sql = “SELECT id FROM cmspages WHERE title = ‘”.mysql_real_escape_string($page).”’ LIMIT 1″;
$result = mysql_query($sql);

if(mysql_num_rows($result))
{
$row = @mysql_fetch_array($result);
$_GET[’page’] = $row[’id’];
}
// Close your DB here …

// Now you have your $_GET[’page’] filled with the requested primary key of the page, now do some permissions checks!
// Remember to always use mysql_real_escape_string !
}
// Mod rewrite[/code:1:5ce86f39af]

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *