No more broken image links!

Hi everybody



You know that we did structure optimization of the images in CS-Cart 2.1. Although it was really a great innovation, some customers complained that they had lost their links, others had found their Google indexation to be decreased as a result of missing image links.



So, here is the solution. After you install the proposed modification, you’ll never lose your image links: an image will always be available at the “old” location. You’ll only need to care about pictures’ unique names.



In order to apply the solution, you’ll need Apache+mod_rewrite and some basic computer skills:


  1. Replace the contents of the /images/.htaccess file in the CS-Cart directory with the below code:


<br />
<IfModule mod_rewrite.c><br />
RewriteEngine on<br />
RewriteRule ^banner/(.*)$ promo/$1<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteRule ^(.*)$ index.php?pattern=$1 [L,QSA]<br />
</IfModule><br />

```<br />
<br />
2) Replace the contents of the /images/index.php file in the CS-Cart directory with the below code:<br />
<br />
```php
<br />
<?php<br />
<br />
if (false === isset($_REQUEST['pattern'])) {<br />
	header('Location: ../index.php');<br />
} else {<br />
<br />
	$files = fn_find_files_with_size(dirname(realpath("index.php")), basename($_REQUEST['pattern']));<br />
<br />
	if (false === empty($files)) {<br />
		asort($files, SORT_NUMERIC);<br />
		end($files);<br />
<br />
		header('Content-Type: ' . fn_mime_content_type(key($files)));<br />
		readfile(key($files));<br />
	}<br />
}<br />
<br />
/** <br />
 * find files matching a pattern <br />
 * using RecursiveDirectoryIterator<br />
 * <br />
 * @return array containing all pattern-matched files <br />
 * <br />
 * @param string $dir     - directory to start with <br />
 * @param string $pattern - pattern to glob for <br />
 */<br />
<br />
function fn_find_files_with_size($dir, $pattern) {<br />
	$result = array();<br />
	$dir_iterator = new RecursiveDirectoryIterator($dir);<br />
	$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);<br />
<br />
	foreach ($iterator as $file_path => $cur) {<br />
		if (basename($file_path) == $pattern) {<br />
			$result[$file_path] = $cur->getSize();<br />
		}<br />
	}<br />
<br />
	return $result;<br />
}<br />
<br />
/**<br />
 * a mime_content_type() replacement<br />
 * <br />
 * see http://www.php.net/manual/en/function.mime-content-type.php<br />
 */<br />
<br />
function fn_mime_content_type($filename) {<br />
<br />
	if(function_exists('mime_content_type')) {<br />
<br />
		return @mime_content_type($filename);<br />
	}<br />
<br />
	$mime_types = array(<br />
		'png' => 'image/png',<br />
		'jpe' => 'image/jpeg',<br />
		'jpeg' => 'image/jpeg',<br />
		'jpg' => 'image/jpeg',<br />
		'gif' => 'image/gif',<br />
		'bmp' => 'image/bmp',<br />
		'ico' => 'image/vnd.microsoft.icon',<br />
		'tiff' => 'image/tiff',<br />
		'tif' => 'image/tiff',<br />
		'svg' => 'image/svg+xml',<br />
		'svgz' => 'image/svg+xml',<br />
	);<br />
<br />
	$ext = strtolower(array_pop(explode('.',$filename)));<br />
<br />
	if (array_key_exists($ext, $mime_types)) {<br />
<br />
		return $mime_types[$ext];<br />
	} elseif (function_exists('finfo_open')) {<br />
		$finfo = finfo_open(FILEINFO_MIME);<br />
		$mimetype = finfo_file($finfo, $filename);<br />
		finfo_close($finfo);<br />
<br />
		return $mimetype;<br />
	} else {<br />
		return 'application/octet-stream';<br />
	}<br />
}<br />
<br />
?><br />

```<br />
<br />
That's all! Now you can test the mod by navigating to a) correct URL of product's image of your choice b) corrupted URL (file name should remain the same)<br />
<br />
Eg., <br />
<br />
[url]http://thebestshopever.com/images/product/0/product_box_1.jpg[/url]<br />
<br />
valid direct URL<br />
<br />
[url]http://thebestshopever.com/images/product/product_box_1.jpg[/url]<br />
[url]http://thebestshopever.com/images/super_product/product_box_1.jpg[/url]<br />
[url]http://thebestshopever.com/images/product/product/product/product_box_1.jpg[/url]<br />
<br />
invalid but still working URLs!

Very nice, thank you!