简体   繁体   English

.htaccess重写除图像,脚本和样式表以外的所有内容

[英].htaccess rewrite everything except images, scripts, and stylesheets

I have a web project with two directories: "core" and "public". 我有一个带有两个目录的Web项目:“核心”和“公共”。

"Core" contains all of the controllers, views, and files required for the Model-View-Controller. “核心”包含“模型-视图-控制器”所​​需的所有控制器,视图和文件。

"Public" contains all public files, like js, css, and less, that can be accessed directly. “公共”包含可以直接访问的所有公共文件,例如js,css和更少。

I have the following .htaccess in the main directory: 我在主目录中具有以下.htaccess

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.less|\.coffee)$
    RewriteRule    ^$    core/    [L]
    RewriteRule    (.*) core/$1    [L]
 </IfModule>

However, it still rewrites those files to the 'core' directory. 但是,它仍然将这些文件重写到“核心”目录中。

What am I doing wrong? 我究竟做错了什么?

I recommend you use this instead: 我建议您改用以下方法:

RewriteEngine on

# First, check if a specific type is being requested
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|css|js|less|coffee)$ [NC]
# Second, check if the request is for an existing file
RewriteCond %{REQUEST_FILENAME} -f
# If both conditions are true, then skip rewriting
RewriteRule ^ - [L]

# Otherwise, continue:
RewriteRule ^$   core/   [L]
RewriteRule (.+) core/$1 [L]

The reason your assets were being sent to core is because the conditions only work for the first rule, which was for your application index. 您的资产被发送到core的原因是,这些条件仅适用于第一个规则,该规则适用于您的应用程序索引。 Using this method tells mod_rewrite to skip rewriting if an asset is being requested. 如果请求资产,则使用此方法告诉mod_rewrite跳过重写。 Once it does that, it can continue with all other rules. 一旦这样做,它就可以继续执行所有其他规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM