我刚刚开始学习htaccess,我想从此重写我现在的网址:
http://www.url.com/?location=script
要:
http://www.url.com/script
到目前为止,我已设法做到这一点,但现在我想拥有一个包含更多控制器的目录,所以我可以这样:
http://www.url.com/script/method
结构:脚本目录 - > method.php
目前我的目录结构包含如下:
资产 - >客户机(目录):
我想使用以下网址访问这些内容:
url.com/client/login
url.com/client/logout
url.com/client/register
url.com/client/something
我的.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?location=$1 [L]
</ifModule>
基于PHP的包含代码:
####################################################################
# PARSE THE CURRENT PAGE #
####################################################################
$includeDir =".".DIRECTORY_SEPARATOR."assets/controllers".DIRECTORY_SEPARATOR;
$includeDefault = $includeDir."home.php";
if(isset($_GET['ajaxpage']) && !empty($_GET['ajaxpage'])){
$_GET['ajaxpage'] = str_replace("\0", '', $_GET['ajaxpage']);
$includeFile = basename(realpath($includeDir.$_GET['ajaxpage'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath)) {
include($includePath);
}
else{
include($includeDefault);
}
exit();
}
if(isset($_GET['location']) && !empty($_GET['location']))
{
$_GET['location'] = str_replace("\0", '', $_GET['location']);
$includeFile = basename(realpath($includeDir.$_GET['location'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath))
{
include($includePath);
}
else
{
include($includeDefault);
}
}
else
{
include($includeDefault);
}
我的所有控制器都在assets / controllers / ucp / login.php中。例如。
答案 0 :(得分:0)
怎么样:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(/client/[a-zA-Z0-9_\-]+)$
RewriteRule ^[a-z]+ index.php?location=$2 [L]
</ifModule>
它确实包含一个前导斜杠,并且没有PHP扩展。但这可以在PHP中进行更改以满足您的需求。
但要小心,您的代码可以访问您的所有PHP文件。您可能想要针对一系列允许的位置检查$ _GET [&#39; location&#39;]。请考虑以下URL作为如何出错的示例
http://example.com/index.php?location=../drop_database.php