[英]Htaccess rewrite rules for this url
我刚刚开始学习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);
}
例如,我所有的控制器都在asset / controllers / ucp / login.php中。
怎么样:
<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 ['location']。 考虑以下URL作为如何可能出错的示例
http://example.com/index.php?location=../drop_database.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.