简体   繁体   English

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

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

I'm building a web api via php. 我正在通过php构建网络api。 One of the things I'm struggling with is setting up the htaccess file to redirect to index.php of the parent folder when something does not exist. 我正在努力的事情之一是设置htaccess文件,以在不存在某些内容时将其重定向到父文件夹的index.php。 My folder structure looks like this: 我的文件夹结构如下所示:

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

This is what I have so far, but it isn't working: 这是我到目前为止的内容,但是没有用:

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

Currently if it doesn't exist it redirects it all the way back to /SkedApi/index.php . 当前,如果不存在,它将一直重定向到/SkedApi/index.php But in the case of /Users/3 I need it to redirect to /Users/index.php . 但是在/Users/3的情况下,我需要它重定向到/Users/index.php /Users/ works correctly and directs it to /Users/index.php . /Users/可以正常工作,并将其定向到/Users/index.php

you can use absolute paths: 您可以使用绝对路径:

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

for users and 对于用户和

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

for the rest. 对于其余的。

Comment: You need the parenthesis in the regexp if you want to reuse that. 注释:如果要重复使用,则需要在正则表达式中使用括号。 Like this: RewriteRule ^User/(.*)$ /User/index.php?uid=$1 [NC,L] Unless yo can go for the simple ^User/ 像这样: RewriteRule ^User/(.*)$ /User/index.php?uid=$1 [NC,L]除非您可以使用简单的^ User /

Edit: if you want to implement some kind of controller logic, you can catch them with regexps. 编辑:如果要实现某种控制器逻辑,则可以使用正则表达式捕获它们。

Considering your controller is a single word: 考虑到您的控制器只是一个字:

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

of course you can omit $1 $2 if your framework gets this info from the query string directly. 当然,如果您的框架直接从查询字符串中获取此信息,则可以省略$ 1 $ 2。

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

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

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