flipzoom / ProcessWire-AIOM-All-In-One-Minify

AIOM+ (All In One Minify) is a ProcessWire module to easily improve the performance of your website. By a simple function call Stylesheets, LESS and Javascript files can be parsed, minimized and combined into one single file. This reduces the server requests, loading time and minimizes the traffic. In addition, the generated HTML source code can be minimized and all generated files can be loaded over a cookieless domain (domain sharding).
MIT License
33 stars 26 forks source link

images into css nof found, error 404 #30

Closed ghost closed 9 years ago

ghost commented 9 years ago

A issue with the path of the images into the css file when pw is installed in a subdirectory, example:

the correct file path output without AIOM+

http://localhost/mysite/site/templates/images/image.jpg

file path output with AIOM+ enable

http://localhost/site/templates/images/image.jpg

this generate 404 error, file not found.

I tried to fix changing the RewriteBase in .htaccess enabling my localhost subdirectory

RewriteBase /mysite/

but the same error still happens if I turn off the AIOM + and use the url from the css file works without any inconvenience..

mbrett5062 commented 9 years ago

Same issue here. Also tried adding to hosts in .htaccess "localhost/mysite" still no luck.

sb3d commented 9 years ago

Here's a couple lines of context (around line 590) followed by my workaround.

// ------------------------------------------------------------------------
// Load source of file and rewrite absolute URLs.
// ------------------------------------------------------------------------
$_css_src   = file_get_contents($stylesheet['absolute_path']).PHP_EOL;
$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path']),wire('config')->paths->root) : $_css_src;

//SB: My fix for issue #30 about paths in css image urls. Compatible with Soma's fix for #20 in which you specify the path in css file starting with /site.  
//Issue 30: When pw is installed in a subdirectory aiom omits the subdirectory from paths it makes for css image urls.
//Allows dev site's pw to be in doc root and deployed site's pw to be in subfolder (or the other way around) and have same css work on both.  
//1. Specify image urls in css without any path (e.g. background:url(T-on75.jpg))
//2. Put the images in templates/styles/    
$rel = str_replace($_SERVER['DOCUMENT_ROOT'], '', wire('config')->paths->templates);    //relative path to templates from doc root. Maybe /subfolder/site/templates 
$_css_src = str_replace('url(/site/templates/styles/', 'url(' . $rel . 'styles/', $_css_src);   //processed url: /subfolder/site/templates/styles/T-off75.jpg
Stikki commented 9 years ago

More elegant way to fix this is simply remove wire document root assumption and expect real DOCUMENT_ROOT to be replaced with Minify_CSS_UriRewriter::rewrite() method.

So simply:

Line 590:

$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path']),wire('config')->paths->root) : $_css_src;

To:

$_css_src = (!empty($_css_src)) ? Minify_CSS_UriRewriter::rewrite($_css_src, dirname($stylesheet['absolute_path'])) : $_css_src;

Fixes this issue, and all URL rewrites are done from DOCUMENT_ROOT instead of wire root

ghost commented 9 years ago

Thanks @Stikki is is a simple and cleaner solution.

marvinscharle commented 9 years ago

Hello everybody,

sorry for my late response. I merged the solution of @Stikki. AIOM has been updated to version 3.1.5. Thank you for your report!