.htaccess rewrite PHP $
.htaccess
RewriteEngine On RewriteRule register index.php?mode=register RewriteRule login index.php?mode=login
index.php
<?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
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 (ie if you have a folder images
then site.com/images will still go to that folder instead of index.php?mode=images
.
上一篇: 解析错误:语法错误,在C:\ wamp \ www \ reg.php中出现意外的'{'
下一篇: .htaccess重写PHP $