【问题标题】:Apache Document Root using .htaccess使用 .htaccess 的 Apache 文档根目录
【发布时间】:2018-08-25 07:25:38
【问题描述】:
我有一个目录结构
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。
【问题讨论】:
标签:
php
apache
.htaccess
mod-rewrite
【解决方案1】:
首先您需要在项目的根目录中创建一个.htaccess 文件。
mymvc/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
然后,在public 目录下的.htaccess 文件中(即mymvc/public/.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]