使用 Apache2 服务器将所有请求重定向到目录及其子目录到目录中的文件


Redirect all requests to a directory and it's subdirectories to a file inside the directory, using Apache2 server

文档文件夹的结构是这样的

/var/wwww/ (root)
    java/
    ...
    php/
        projectone/
            controller/
            view/
                index.php
            ...
        projecttwo/
            ...

我希望所有对/php/projectone 或其子文件夹的请求都重定向到/php/projectone/view/index.php,而对/php/projecttwo 和其他文件夹的请求不应重定向。

我相信使用mod_rewrite是方法,但是,我仍然没有实现我想要的。

使用以下

规则将.htaccess放在项目一个文件夹中:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /php/projectone/
RewriteRule ^(view/)?index'.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . view/index.php [L]

您可以对拥有的任何其他项目文件夹重复此操作。 通过更改RewriteBase .