简体   繁体   English

如何使用mod_Rewrite检查多个文件夹中的静态文件

[英]How to use mod_Rewrite to check multiple folders for a static file

What are the mod_Rewrite rules for checking multiple folder locations for a given file. 用于检查给定文件的多个文件夹位置的mod_Rewrite规则是什么。 For instance, if I have a folder structure of: 例如,如果我有一个文件夹结构:

public/
    css/
    library1/
        css/
    library2/
        css/

and I want requests to /css/somefile.css to first check the public/css/ directory, then cascade to public/library1/css/ then public/library2/css/ , returning a 404 if an asset cannot be found in any of the directories. 我希望请求/css/somefile.css首先检查public/css/目录,然后级联到public/library1/css/ then public/library2/css/ ,如果在任何一个资源中找不到资产,则返回404目录。

I was thinking along the lines of: 我在思考:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond library1%{REQUEST_URI} -f
RewriteRule ^(.*)$ library1$1 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond library2%{REQUEST_URI} -f
RewriteRule ^(.*)$ library2$1 [L]

But this doesn't seem to work - I'm not sure how to check for file existence on a dynamically generated path. 但这似乎不起作用 - 我不确定如何在动态生成的路径上检查文件是否存在。

Try these rules: 试试这些规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/library1%{REQUEST_URI} -f
RewriteRule ^css/.+ library1%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/library2%{REQUEST_URI} -f
RewriteRule ^css/.+ library2%{REQUEST_URI} [L]

RewriteMap指令可能有所帮助。

Possibly the server variables do not contain what you think. 可能服务器变量不包含您的想法。 Try increase the logging to debug, so you can see exactly what is going on. 尝试将日志记录增加到调试,这样您就可以确切地看到正在发生的事情。

Try this: 尝试这个:

# Loads files from production server
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (library1|library2)/(.*)$ http://production.com/$1/$2 [R=302,L,NC]

To achieve this: 为达到这个:

something.dev/ library1 /style.css -> production.com/ library1 / style.css something.dev/ library2 / vendor/css/style.css -> production.com/ library2 / vendor/css/style.css something.dev/ LIBRARY1 /style.css - > production.com/ LIBRARY1 / style.css中 something.dev/ library2 / 供应商/ CSS / style.css中 - > production.com/ library2 / 供应商/ CSS / style.css中

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

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