Jump to content

CSS 404 error caused by Friendly URL


Atanda

Recommended Posts

Hello.

 

I have a module that includes a CSS file into the header of my Prestashop site using this code: 

$('head').append('<link href="modules/mymodule/style.css" rel="stylesheet" type="text/css" media="all">');

Without turning on Friendly url or using multiple languages, the url works well. But immediately one turns on friendly url and multiple languages, the browser shows error that css file cannot be found.

 

I found out that the friendly url changes the link to the css file so it cannot be loaded. 

 

 

This is the right url - 

http://myshop/modules/mymodule/style1.css

This are the wrong ones with friendly url - 

http://myshop/en/modules/mymodule/style1.css

or 

http://myshop/en/clothing/modules/mymodule/style1.css

As you can see, the css file cannot be loaded becuase "en" and "clothing" has been added to the link to get the css file.

 

 

Please how can write the javascript code above so that it gives the correct link when friendly url or multiple language is turned on.

 

Thank you for your help.

Edited by Atanda (see edit history)
Link to comment
Share on other sites

 

in the module, depending on the ps version,  you would add .css for example  like this

	public function hookHeader()
	{
			if (substr(_PS_VERSION_, 0, 3) == '1.5') 
				$this->context->controller->addCSS(($this->_path).'css/filename.css', 'all');
			else
				Tools::addCSS(($this->_path).'css/filename.css', 'all');

	}

 

Thank you very much for this suggestion. I understand this part but this module is used for adding various color styles to the prestashop theme. I think that is why it is written this way. The full code looks like this:

$('head').append('<link href="modules/mymodule/css/'+ ($.cookie("template_color"))+'" rel="stylesheet" type="text/css" media="all">');

There are different styles like style1.css, style2.css, etc. This code is also added to a tpl file. Could their be something that can be added to this code so that it will stop the friendly url from changing the path?

 

Thank you very much.

Edited by Atanda (see edit history)
Link to comment
Share on other sites

hrllo

 

in this case you have to use full url to the file (with http)

 

Thank you very much sirs! This solution works well even though not perfectly!

 

I wish a code like {$base_dir} will work so that I can use the module on more than one store without manually adding full url for each one. Sadly, {$base_dir} does not work.

 

Thank you all the same!

Link to comment
Share on other sites

have you tried to use {$content_dir} ?

 

I tried that too. It did not work. Seems it these do not work within the head tag. I got this:

 

localhost/prestashop/en/%7B$content_dir%7Dmodules/mymodule/css/style1.css

 

instead of this:

 

localhost/prestashop/modules/mymodule/css/style1.css

 

:(

Edited by Atanda (see edit history)
Link to comment
Share on other sites

  • 2 months later...

Thank you very much sirs! This solution works well even though not perfectly!

 

I wish a code like {$base_dir} will work so that I can use the module on more than one store without manually adding full url for each one. Sadly, {$base_dir} does not work.

 

Thank you all the same!

base dir works well with my shop. did you try it online? cause there are some differences always between local and online!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...