繁体   English   中英

htaccess重定向到父文件夹index.php(如果不存在)

[英]htaccess redirect to parent folder index.php if does not exist

我正在通过php构建网络api。 我正在努力的事情之一是设置htaccess文件,以在不存在某些内容时将其重定向到父文件夹的index.php。 我的文件夹结构如下所示:

/SkedApi/
    index.php
    /Users/
        index.php

这是我到目前为止的内容,但是没有用:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]

当前,如果不存在,它将一直重定向到/SkedApi/index.php 但是在/Users/3的情况下,我需要它重定向到/Users/index.php /Users/可以正常工作,并将其定向到/Users/index.php

您可以使用绝对路径:

RewriteRule ^Users/(.*)$ /Users/index.php [NC,L] 

对于用户和

RewriteRule ^(.*)$ /index.php [NC,L] 

对于其余的。

注释:如果要重复使用,则需要在正则表达式中使用括号。 像这样: RewriteRule ^User/(.*)$ /User/index.php?uid=$1 [NC,L]除非您可以使用简单的^ User /

编辑:如果要实现某种控制器逻辑,则可以使用正则表达式捕获它们。

考虑到您的控制器只是一个字:

RewriteRule ^([^/]*)/(.*)$ /Users/index.php?mycontroler=$1&mydata=$2 [NC,L] 

当然,如果您的框架直接从查询字符串中获取此信息,则可以省略$ 1 $ 2。

RewriteRule ^([^/]*) /Users/index.php [NC,L] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM