MVC公用文件夹htaccess无法正常工作 [英] MVC public folder htaccess not working

查看:87
本文介绍了MVC公用文件夹htaccess无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的index.php(处理路由的文件)和css/js/font资产移到了public/文件夹中.为了安全起见,我只希望访问此public/文件夹中的项目.当我意识到可以访问mysite.com/composer.lock并使用旧的.htaccess和文件夹设置查看作曲家锁定文件时,我遇到了这个问题.

I recently moved my index.php (the file that handles routing) and css/js/font assets to a public/ folder. I only want items in this public/ folder to be accessible for security purposes. I ran into this problem when I realized I could visit mysite.com/composer.lock and view the composer lock file with my old .htaccess and folder setup.

这就是我想要的

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

If I visit mysite.com/car/create, I want it to actually point to mysite.com/public/index.php?path=car/create

如果我链接到诸如mysite.com/css/style.css之类的资产,我希望它确实指向mysite.com/public/css/style.css

If I link to an asset such as mysite.com/css/style.css, I want it to really point to mysite.com/public/css/style.css

这是我的文件夹结构

这是我的.htaccess根本无法正常工作

Here is my .htaccess which is not working at all

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

我不确定如何解决此问题.它只会生成空白页,我仍然可以直接访问根目录等中的文件.我们将为您提供任何帮助.

I'm not sure how to fix this. It just generates empty pages and I can still directly visit files in the root directory etc. Any help is appreciated.

推荐答案

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

Your existing directives specifically avoid rewriting requests for existing files, so it would still enable you to visit files in the root directory. It will also rewrite static resources to public/index.php?path=, which will presumably fail.

请尝试以下操作:

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]

这篇关于MVC公用文件夹htaccess无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆