.htaccess
RewriteEngine On
RewriteRule register index.php?mode=register
RewriteRule login index.php?mode=login
index.php
if ( isset ( $_GET['mode']) && ( $_GET['mode'] == 'register' ) ) {
include('includes/register.php');
} elseif ( isset ( $_GET['mode']) && ( $_GET['mode'] == 'login' ) ) {
include('includes/login.php');
}
?>
This is my current method (thanks to @TROODON).
Is there an easier way, maybe using key-value arrays to store all the possibilities for the various pages that index.php will call?
Thanks
Answer
For your .htaccess
you can do this:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?mode=$1 [L,QSA]
However, don't change your PHP code to just include
whatever you are getting from $_GET['mode']
! This will allow users to include at will.
You could adjust your PHP code like so:
$pages = array("register" => "includes/register.php",
"login" => "includes/login.php");
if(isset($_GET['mode']) && $pages[$_GET['mode']])
include $pages[$_GET['mode']];
PS: The two RewriteCond
's make sure the url is not an existing file or folder (i.e. if you have a folder images
then site.com/images will still go to that folder instead of index.php?mode=images
.
No comments:
Post a Comment