【发布时间】:2015-05-11 12:06:38
【问题描述】:
我刚开始学习 htaccess,我想从这里重写我当前的网址:
http://www.url.com/?location=script
收件人:
http://www.url.com/script
到目前为止,我已经设法做到了,但现在我想要一个包含更多控制器的目录,这样我就可以拥有这样的东西:
http://www.url.com/script/method
结构:脚本目录 --> method.php
目前我的目录结构是这样的:
资产-->客户(目录):
- login.php
- logout.php
- register.php
- something.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 中。
【问题讨论】:
-
不回答你的问题,考虑在PSR-2 guide之后编写你的PHP。其他 PHP 程序员可能更愿意帮助您。
-
以后我会考虑你的建议。