首页 文章

Apache htaccess从一个目录或另一个目录加载文件

提问于
浏览
0

我的目标是如果在第一个目录中找不到请求的文件,只需让Apache第二次重写URL .

现在我有以下Apache重写,这很好...

RewriteRule ^[^/]*/scripts(.+) scripts$1

这使得所有域的所有JavaScript文件请求都访问以下目录中的文件...

www / scripts /


我的目录/文件结构......

www / www / scripts / www / www.example.com / www / www.example.com / scripts /

我需要做的是如果请求的文件不在此目录中...

www / www.example.com / scripts /

...然后让Apache看看这个目录......

www / scripts /


这是为了 localhost 本地开发,所以请求URL看起来像......

http:// localhost / www.example.com / scripts / index.js


这是我一直在修补的一个例子......

RewriteCond %{REQUEST_FILENAME} (.*)\/scripts\/\.(js)$
RewriteCond ^[^/]*scripts$2 -f
RewriteRule ^[^/]*/scripts(.+) scripts$2[L]

1 回答

  • 1

    希望能让您更接近解决方案 . 这是我未经考验的想法:

    # check if we're looking for a script in that directory
    RewriteCond %{REQUEST_FILENAME} www\/www\.example\.com\/scripts\/(.*)\.js$
    
    # check if that file DOESN'T exist
    RewriteCond www\/www\.example\.com\/scripts\/%1\.js !-f
    
    # it doesn't exist - so try to rewrite that file to www/scripts
    RewriteRule www\/www\.example\.com\/scripts\/(.+)\.js$ www/scripts/$1.js
    

    祝好运

相关问题