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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败