php - MVC 公共(public)文件夹 htaccess 不工作

标签 php apache .htaccess mod-rewrite

我最近将我的 index.php(处理路由的文件)和 CSS、JavaScript 和字体资源移到了 public/ 文件夹中。出于安全目的,我只希望可以访问此 public/ 文件夹中的项目。当我意识到我可以访问 mysite.com/composer.lock 并使用我的旧 .htaccess 和文件夹设置查看 Composer 锁文件时,我遇到了这个问题。

这是我想要的

如果我访问 mysite.com/car/create,我希望它实际指向 mysite.com/public/index.php?path=car/create

如果我链接到诸如 mysite.com/css/style.css 之类的 Assets ,我希望它真正指向 mysite.com/public/css/style.css

这是我的文件夹结构

MVC Folder Structure

这是我的 .htaccess,它根本不起作用

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ public/index.php?path=$1 [L,QSA]
</IfModule>

我该如何解决这个问题?它只是生成空页面,我仍然可以直接访问根目录中的文件等。

最佳答案

您现有的指令专门避免重写对现有文件的请求,因此它仍然可以让您访问根目录中的文件。它还会将静态资源重写为 public/index.php?path=,这可能会失败。

请尝试以下操作:

RewriteEngine On

# Stop processing if already in the /public directory
RewriteRule ^public/ - [L]

# Static resources if they exist
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule (.+) public/$1 [L]

# Route all other requests
RewriteRule (.*) public/index.php?path=$1 [L,QSA]

关于php - MVC 公共(public)文件夹 htaccess 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41516541/

相关文章:

php - 我可以从 MySQL 数据库中进行选择,但是当我运行此插入时没有任何反应(仅插入到 1 个表中)

php - 从经纬度获取国家名称

php - 如何在 .htaccess 中编写 AddHandler?

php - .htaccess 重写以问号开头的请求 url

apache - .htaccess 重写用户名(例如 facebook)

php - .css 样式表中的 mod_rewrite 图像现在需要绝对路径吗?

php - Wordpress 资源被解释为样式表但使用 mime 类型 text/html 传输

php - 一个带有 div 和多个 li 的 foreach 循环

linux - 如何在不设置 777 权限的情况下使 apache 读写用户目录

apache - OpenNLP名称查找器