我正在开发一个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.php
、license.txt
或i/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]