dongluxin2452 2015-11-10 05:49
浏览 236
已采纳

某些REQUEST_URI上的mod_rewrite

I have a file structure like this:

ROOT
|----app/
|----app/init.php
|----public/
|----public/index.php
|----public/img/
|----public/js/
|----.htaccess

The .htaccess's rules will redirect all REQUEST_URI to app/init.php

RewriteCond %{REQUEST_URI} !\.(css|js|ttf|woff|woff2|eot|otf|svg|png|jpg)$
RewriteCond public/%{REQUEST_FILENAME} !-f
RewriteCond public/%{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ app/init.php [NC,L,QSA]

Then app/init.php will use require_once to get the appropriate .html/.php in the public folder.

This works fine; all excluded file in .htaccess will be called as such public/... pattern of path in public/index.php.

For examples:

  1. CSS <link rel="stylesheet" href="public/css/layout.css">
  2. Image <img src="public/img/abc.jpg" alt="">

However, I don't want to add public/ prefix on all the file I excluded in .htaccess

I tried adding

RewriteCond %{REQUEST_URI} \.(jpg)$
RewriteRule ^([^?]*)$ public/$1 [NC]

However, it does not work. More accurately, when I called like this img/abc.jpg in public/index.php file, it feeds back is 500 (Internal Server Error). I am guessing it is something wrong with the redirection, but I am not sure.

Question: How can I call css or img or etc.. those file without a prefix of public/ in the path?

  • 写回答

1条回答 默认 最新

  • dpd20130 2015-11-10 08:00
    关注

    Have it this way in your root .htaccess:

    RewriteEngine On
    
    # handle css, js and img files
    RewriteRule (?:^|/)((?:img|css|js)/.+) public/$1 [L,NC]
    
    # handle rest of the files
    RewriteCond public/%{REQUEST_FILENAME} !-f
    RewriteCond public/%{REQUEST_FILENAME} !-d
    RewriteRule !^public/ app/init.php [L,NC]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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