Php htaccess请求为';文件夹';

Php htaccess请求为';文件夹';,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我遇到了一个问题,我试图将url映射到文件夹(虚拟文件夹), 因此,我尝试而不是http://site.eu/controlpanel.php?username=Username&do=services 将其映射为http://site.eu/cp/user/services 我正在使用以下代码 重写规则^cp/([^/]+)/([^/]+)/controlpanel.php?用户名=$1&do=$2[L,QSA,NC] 如果我想访问,除了两件事,一切都好 http://site.eu/cp/u

我遇到了一个问题,我试图将url映射到文件夹(虚拟文件夹), 因此,我尝试而不是
http://site.eu/controlpanel.php?username=Username&do=services
将其映射为
http://site.eu/cp/user/services

我正在使用以下代码
重写规则^cp/([^/]+)/([^/]+)/controlpanel.php?用户名=$1&do=$2[L,QSA,NC]

如果我想访问,除了两件事,一切都好

http://site.eu/cp/user/
它给我404错误(为什么?
此外,如果我试图访问比

http://site.eu/cp/user/services/

这给了我同样的错误

在php文件中,我使用这个结构

<?php
switch($_REQUEST['do'])
{
   case 'services':
    some code to display page;
   break;

}
?>

我不知道如何在/cp/user/services/。。。可供用户使用


请帮助我

您需要将第二个参数设置为可选,并匹配多个路径节点:

 RewriteRule ^cp/([^/]+)/(?:(.+)|)$ /controlpanel.php?username=$1&do=$2 [L,QSA,NC]

您可以改为使用以下结构:

RewriteRule ^cp/([^/]+)/?(.*)$ /controlpanel.php?username=$1&path=$2 [L,QSA,NC]
因此,如果您使用URL:

http://site.eu/cp/user/services/ServiceID/Action

…您将被发送到:

http://site.eu/controlpanel.php?username=user&path=services/ServiceID/Action

然后,在PHP文件中:

<?php
$path = ($_REQUEST['path']) ? explode('/', strtolower($_REQUEST['path'])) : array();

if (!empty($path)) { // A path is established; find the page.

    if ($path[0] === 'services') {
        if (!empty($path[1])) { // Visitor is at '/cp/user/services'.
            // ...
        }
        else if (ctype_digit($path[1])) { // Visitor is at '/cp/user/services/[ServiceId]'.
            // ...
        }
        // ...
    }

}
else { // No path exists. Visitor is at '/cp/user'.
    // ...
}
?>


这里的问题是,如果用户打开/cp/user/services/-than它认为路径[1]已设置,但是如果/cp/users/services-than它打开了服务页面,服务id-自动生成的服务编号,因此我无法将其与任何变量或字符串匹配使用
是数字($path[1])而不是
isset($path[1])
如果将
$path[1]
的第一个条件更改为
if(!empty($path[1])
,则此操作应该会更好。对于第二个条件,您应该使用而不是
is\u numeric()
,因为
is\u numeric()
将允许十进制/浮点值,但是
ctype\u digit()
将只允许整数。查看我对答案的编辑。
RewriteRule ^cp/([^/]+)/?(.*)$ /controlpanel.php?username=$1&path=$2 [L,QSA,NC]
<?php
$path = ($_REQUEST['path']) ? explode('/', strtolower($_REQUEST['path'])) : array();

if (!empty($path)) { // A path is established; find the page.

    if ($path[0] === 'services') {
        if (!empty($path[1])) { // Visitor is at '/cp/user/services'.
            // ...
        }
        else if (ctype_digit($path[1])) { // Visitor is at '/cp/user/services/[ServiceId]'.
            // ...
        }
        // ...
    }

}
else { // No path exists. Visitor is at '/cp/user'.
    // ...
}
?>