我有一个目录结构
mymvc
|--App
|--Core
|--logs
|--public
|--index.php
|--vendor
|--.htaccess
我想要的是,如果有人点击我的网址 www.example.com/mymvc/
那么所有请求都必须通过 public->index.php
使用 .htaccess
文件。我无权访问 httpd.conf 文件。
|--public
|--index.php
我希望我的 public
文件夹只能作为文档根访问,请求通过 public
文件夹内的 index.php 文件。没有人可以直接访问 App
、 Core
、 logs
等目录。意思是我希望我的公用文件夹是 DOCUMENT ROOT
。
最佳答案
首先您需要在项目的根目录中创建一个.htaccess
文件。
mymvc/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
然后,在 public
目录(即 mymvc/public/.htaccess)的 .htaccess
文件中,您需要将 RewriteBase
指令添加到现有代码中,因此它看起来像这样:
mymvc/public/.htaccess
# Remove the question mark from the request but maintain the query string
RewriteEngine On
RewriteBase /mymvc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
关于php - 使用 .htaccess 的 Apache 文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52015013/