dsj1961061 2017-01-07 00:33
浏览 44
已采纳

MVC公用文件夹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.

Here is what I want

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

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

Here is my folder structure

MVC Folder Structure

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.

  • 写回答

1条回答 默认 最新

  • doudong4532 2017-01-07 01:42
    关注

    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.

    Try the following instead:

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭