如何使用 htaccess 将所有非公共文件请求重定向到我的公共索引脚本?



我正在开发一个Web应用程序,我想为它提供一个基本的.htaccess文件。我决定这个结构会很好:

-/
├ .git
├ private
| └ lib.php
├ public
| ├ index.php
| └ css
|   └ app.css
├ .editorconfig
├ .gitignore
├ .htaccess
├ license.txt
└ readme.txt

我已经尝试了很多配置,其中许多都使用mod_rewrite,但我还没有找到任何一种完全符合我正在寻找的配置:">所有不要求公共目录下文件的请求都必须由public/index.php处理"。这意味着对private/lib.phplicense.txti/do/not/exist.php的请求将由public/index.php处理,但对public/css/app.css的请求将直接返回该文件。

例。作为服务器根目录上的项目:

http://localhost -> runs public/index.php
http://localhost/hello -> runs public/index.php
http://localhost/license.txt -> runs public/index.php
http://localhost/private/lib.php -> runs public/index.php
http://localhost/public/css -> runs public/index.php
http://localhost/public/css/app.css -> shows public/css/app.css contents

你可以帮我吗?提前谢谢。

您可以按如下方式.htaccess站点根目录:

RewriteEngine On
# by default show public/index.php
RewriteRule ^$ public/index.php [L]
# if requesting any other dir/file then show public/index.php
RewriteRule !^public/ public/index.php [L,NC]
# for all non-files show public/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]

最新更新