Page 1 of 2

No base URL for images in sitemap.xml

Posted: Tue Jun 14, 2016 10:03 pm
by R_D
Hi,

I've got a strange problem. I've got OC 2.2 installed and forced it into https
Everything works fine except the sitemap

When I test it at Google Webmaster Tools I receive an error displaying that the url for images is invalid. This because the base url of my store is missing.

The url in sitemap is: image/cache/catalog/category/product.jpg
instead of : https://www.store.com/image/cache/catal ... roduct.jpg

Does anyone have an idea?

Thanks!

Re: No base URL for images in sitemap.xml

Posted: Wed Jun 15, 2016 12:02 am
by oc-extensions
Hi,

When sitemap.xml is accessed from .htaccess file will "redirect" google to feed/google_sitemap

Code: Select all

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap
feed/google_sitemap will generate full URL for images

Code: Select all

$output .= '<image:loc>' . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
If image URL is generated without BASE URL then:
- config.php is wrong
- or you modified image class directly or from some vqmod/ocmod extension
- many other reasons -

You should ask an developer to take a look

Good Luck!

Re: No base URL for images in sitemap.xml

Posted: Wed Jun 15, 2016 1:05 am
by R_D
oc-extensions wrote:Hi,

When sitemap.xml is accessed from .htaccess file will "redirect" google to feed/google_sitemap

Code: Select all

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap
feed/google_sitemap will generate full URL for images

Code: Select all

$output .= '<image:loc>' . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
If image URL is generated without BASE URL then:
- config.php is wrong
- or you modified image class directly or from some vqmod/ocmod extension
- many other reasons -

You should ask an developer to take a look

Good Luck!

Thanks for answering, config.php is correct and I didn't modify image class directly or from some vqmod/ocmod extension.

I think it must be the "many other reasons" :-[
Strangely though, the images are displayed ok in the store. ???

Re: No base URL for images in sitemap.xml

Posted: Wed Jun 15, 2016 1:13 am
by oc-extensions
Hi,

When browser is used on yourstore.com and in page images are loaded like "image/something.png" are displayed correctly (with relative path) because browser convert path to yourstore.com/image/something.php

In your store take a look with view source and check images path (is absolute path or relative path)?

To clarify:
absolute path = "http://www.yourstore.com/image/something.jpg"
relative path = "image/something.jpg"

Re: No base URL for images in sitemap.xml

Posted: Wed Jun 15, 2016 4:00 am
by R_D
oc-extensions wrote:Hi,

When browser is used on yourstore.com and in page images are loaded like "image/something.png" are displayed correctly (with relative path) because browser convert path to yourstore.com/image/something.php

In your store take a look with view source and check images path (is absolute path or relative path)?

To clarify:
absolute path = "http://www.yourstore.com/image/something.jpg"
relative path = "image/something.jpg"

You're right: it's showing the relative path... :'(
Back to the drawing board I guess ;)

Re: No base URL for images in sitemap.xml

Posted: Wed Jun 15, 2016 5:02 pm
by R_D
oc-extensions wrote:Hi,

When browser is used on yourstore.com and in page images are loaded like "image/something.png" are displayed correctly (with relative path) because browser convert path to yourstore.com/image/something.php

In your store take a look with view source and check images path (is absolute path or relative path)?

To clarify:
absolute path = "http://www.yourstore.com/image/something.jpg"
relative path = "image/something.jpg"

Just a question: do you know of a way to insert the base url temporarely so I can use the sitemap while searching for the error?

Thanks in advance!

Re: No base URL for images in sitemap.xml

Posted: Thu Jun 16, 2016 5:23 am
by uksitebuilder
Add a function in the controller:

catalog/controller/feed/google_sitemap.php

Code: Select all

	public function serverURL(){
		$isSecure = false;
		if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
			$isSecure = true;
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
			$isSecure = true;
		}
		return ($isSecure ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'].'/';
	}
Then prefix the image url with: $this->serverURL() . 'image/….…...

Re: No base URL for images in sitemap.xml

Posted: Thu Jun 16, 2016 2:14 pm
by R_D
uksitebuilder wrote:Add a function in the controller

Code: Select all

	public function serverURL(){
		$isSecure = false;
		if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
			$isSecure = true;
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
			$isSecure = true;
		}
		return ($isSecure ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'].'/';
	}
Then prefix the image url with: $this->serverURL() . 'image/….…...

I will try that, thanks!

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 12:51 pm
by ahill16
uksitebuilder wrote:Add a function in the controller

Code: Select all

	public function serverURL(){
		$isSecure = false;
		if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
			$isSecure = true;
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
			$isSecure = true;
		}
		return ($isSecure ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'].'/';
	}
Then prefix the image url with: $this->serverURL() . 'image/….…...
what controller file does this go into? I am having the same issue

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 1:48 pm
by uksitebuilder
catalog/controller/feed/google_sitemap.php

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 2:25 pm
by ahill16
uksitebuilder wrote:catalog/controller/feed/google_sitemap.php
I attempted to put this there but it didnt work. Do i Just copy and paste the code in the very bottom of the file?

When i posted the code at the very end me sitemap showed a url but still did not have the full url for the image path. Is there any way to edit how opencart creates the sitemap image urls?

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 5:38 pm
by uksitebuilder
Did you amend the image code in the file

from

'image/….….….….…...

To

$this->serverURL() . 'image/….…...

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 9:36 pm
by ahill16
can you give me a hand with the code? I havent touched code much outside classic ASP.

Code: Select all

<?php
class ControllerFeedGoogleSitemap extends Controller {
	public function index() {
		if ($this->config->get('google_sitemap_status')) {
			$output  = '<?xml version="1.0" encoding="UTF-8"?>';
			$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';

			$this->load->model('catalog/product');
			$this->load->model('tool/image');

			$products = $this->model_catalog_product->getProducts();

			foreach ($products as $product) {
				if ($product['image']) {
					$output .= '<url>';
					$output .= '<loc>' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . '</loc>';
					$output .= '<changefreq>weekly</changefreq>';
					$output .= '<lastmod>' . date('Y-m-d\TH:i:sP', strtotime($product['date_modified'])) . '</lastmod>';
					$output .= '<priority>1.0</priority>';
					$output .= '<image:image>';
					$output .= '<image:loc>' . $this->serverURL() . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
					$output .= '<image:caption>' . $product['name'] . '</image:caption>';
					$output .= '<image:title>' . $product['name'] . '</image:title>';
					$output .= '</image:image>';
					$output .= '</url>';
				}
			}

			$this->load->model('catalog/category');

			$output .= $this->getCategories(0);

			$this->load->model('catalog/manufacturer');

			$manufacturers = $this->model_catalog_manufacturer->getManufacturers();

			foreach ($manufacturers as $manufacturer) {
				$output .= '<url>';
				$output .= '<loc>' . $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $manufacturer['manufacturer_id']) . '</loc>';
				$output .= '<changefreq>weekly</changefreq>';
				$output .= '<priority>0.7</priority>';
				$output .= '</url>';

				$products = $this->model_catalog_product->getProducts(array('filter_manufacturer_id' => $manufacturer['manufacturer_id']));

				foreach ($products as $product) {
					$output .= '<url>';
					$output .= '<loc>' . $this->url->link('product/product', 'manufacturer_id=' . $manufacturer['manufacturer_id'] . '&product_id=' . $product['product_id']) . '</loc>';
					$output .= '<changefreq>weekly</changefreq>';
					$output .= '<priority>1.0</priority>';
					$output .= '</url>';
				}
			}

			$this->load->model('catalog/information');

			$informations = $this->model_catalog_information->getInformations();

			foreach ($informations as $information) {
				$output .= '<url>';
				$output .= '<loc>' . $this->url->link('information/information', 'information_id=' . $information['information_id']) . '</loc>';
				$output .= '<changefreq>weekly</changefreq>';
				$output .= '<priority>0.5</priority>';
				$output .= '</url>';
			}

			$output .= '</urlset>';

			$this->response->addHeader('Content-Type: application/xml');
			$this->response->setOutput($output);
		}
	}

	protected function getCategories($parent_id, $current_path = '') {
		$output = '';

		$results = $this->model_catalog_category->getCategories($parent_id);

		foreach ($results as $result) {
			if (!$current_path) {
				$new_path = $result['category_id'];
			} else {
				$new_path = $current_path . '_' . $result['category_id'];
			}

			$output .= '<url>';
			$output .= '<loc>' . $this->url->link('product/category', 'path=' . $new_path) . '</loc>';
			$output .= '<changefreq>weekly</changefreq>';
			$output .= '<priority>0.7</priority>';
			$output .= '</url>';

			$products = $this->model_catalog_product->getProducts(array('filter_category_id' => $result['category_id']));

			foreach ($products as $product) {
				$output .= '<url>';
				$output .= '<loc>' . $this->url->link('product/product', 'path=' . $new_path . '&product_id=' . $product['product_id']) . '</loc>';
				$output .= '<changefreq>weekly</changefreq>';
				$output .= '<priority>1.0</priority>';
				$output .= '</url>';
			}

			$output .= $this->getCategories($result['category_id'], $new_path);
		}

		return $output;
	}
   public function serverURL(){
      $isSecure = false;
      if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
         $isSecure = true;
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
         $isSecure = true;
      }
      return ($isSecure ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'].'/';
   }
}

Re: No base URL for images in sitemap.xml

Posted: Thu Aug 11, 2016 10:24 pm
by uksitebuilder
Hi

I have modified your code above.

Re: No base URL for images in sitemap.xml

Posted: Fri Aug 12, 2016 10:34 am
by ahill16
this is still a nogo. sitemap is still generating the short url and not the long one

here is the sitemap that it gives me. I copied and pasted the new code that was in the box

https://www.uslawnparts.com/test/index. ... le_sitemap

Re: No base URL for images in sitemap.xml

Posted: Fri Aug 12, 2016 11:49 am
by ahill16
uksitebuilder wrote:Did you amend the image code in the file

from

'image/….….….….…...

To

$this->serverURL() . 'image/….…...

Can you show me an example of where this goes? does this go in my image name when i upload images?

Re: No base URL for images in sitemap.xml

Posted: Fri Aug 12, 2016 1:26 pm
by uksitebuilder
I put it in the code above that I amended in your post

Code: Select all

$output .= '<image:loc>' . $this->serverURL() . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
Of course you can amend it further by hardcoding your url instead of using the function if you wish

change the above to

Code: Select all

$output .= '<image:loc>http://ENTERYOURDOMAINHERE.COM/' . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';

Re: No base URL for images in sitemap.xml

Posted: Fri Aug 12, 2016 9:06 pm
by ahill16
uksitebuilder wrote:I put it in the code above that I amended in your post

Code: Select all

$output .= '<image:loc>' . $this->serverURL() . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
Of course you can amend it further by hardcoding your url instead of using the function if you wish

change the above to

Code: Select all

$output .= '<image:loc>http://ENTERYOURDOMAINHERE.COM/' . $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
So if i Put this into the code into the file and it still isnt doing anything. Could it be google preventing this. The sitemap generates from opencart right?

Re: No base URL for images in sitemap.xml

Posted: Fri Aug 12, 2016 9:38 pm
by uksitebuilder
Most likely you are serving a cached page. Check your caching extensions if you have any that may be caching the file.

Some extensions offer an exclusions list where you can set the feed url to be excluded from caching.

Re: No base URL for images in sitemap.xml

Posted: Sat Aug 13, 2016 1:12 am
by ahill16
uksitebuilder wrote:Most likely you are serving a cached page. Check your caching extensions if you have any that may be caching the file.

Some extensions offer an exclusions list where you can set the feed url to be excluded from caching.
Are you talking about a cached page in my browser or in my site files?