使用.htaccess将用户从任何没有index.php的文件夹路由到一些文件,如:index.php


Route user to some file like: index.php from any folder which is not having index.php | using .htaccess

我的.htaccess文件中有此代码,用于将用户路由到我的root/index.php文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

然而,这很好:所以如果我要去localhost/MyFolder/XyzXyz is not existing it takes me to localhost/index.php现在我想要的是,我有像css js images这样的文件夹,所以我没有任何index.php文件,所以它列出了我所有的文件。我知道我可以在那里添加index.php文件,并可以将用户重定向到我的根目录。但这是TDS的工作。有没有什么方法可以让我在.htaccess中做一些事情?如果在任何文件夹中找不到index.php,我会去localhostindex.php

有没有一种方法可以做到。只是不想把index.php放在每个文件夹中以限制其访问。

当没有索引文件时,您基本上想要的是避免列出文件夹中的所有文件。

您可以通过将其添加到.htaccess文件中来实现这一点:

Options All -Indexes

好的,我认为您想要的只是从.htaccess文件中删除"重写条件"。如果输入的路径是文件或目录,这些重写条件会告诉Apache不要重定向。

RewriteEngine On
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

就是这样。