urlManager relative path problem

Hi All,

I greatly appreciate any help, because this has be baffled right now.

When I specify ‘urlFormat’=>‘path’ in urlManager in main.php, relative paths for image src’s and CSS relative url paths break. It’s strange because it doesn’t happen on the homepage. I have the urlManager set as follows:




'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName'=>false,

	'rules'=>array(

		// REST pattern

		'api/<action:\w+>'=>'api/<action>',


		'about'=>'site/page/view/about',

		'help'=>'site/page/view/help',

		'terms'=>'site/page/view/terms',

		'privacy'=>'site/page/view/privacy',

		'contact'=>'site/contact',

		'feedback'=>'site/feedback',

	),

),



When I comment out the ‘urlFormat’=>‘path’, the relative paths to images in HTML and CSS work.

The relative paths work on the pages “about”, “help”, “terms”, “privacy”, “contact”, and “feedback”, but only when the rules specified for each of those pages are set and ‘urlFormat’=>‘path’ is set. If I comment out the rules for the pages just listed, then the relative paths for image src’s and CSS relative url paths break on those pages as well (if ‘urlFormat’=>‘path’ is set).

Anybody have any clues?

Example relative paths:


<img src="images/logo.png" />

and


background-image: url("images/icons/1611.jpg");

Any help appreciated.

Thank you!

For the images referenced in <img> tags, do they display correctly if you try:


<img src="<?php echo Yii::app()->baseUrl; ?>/images/logo.png" />

I reckon the relative paths would be breaking when you don’t have each individual rule specified because the path urlFormat would be looking for an controller named images and an action named logo.png (I think).

Adding the baseUrl as you specified DOES work. But what do I do for CSS urls?

Is this the normal procedure when the urlManager is enabled with ‘urlFormat’=>‘path’?

CSS URLs usually works if it’s “../images/image.png” - just try it. Works for me. ;)

I thought that might work, but alas, it does not :(

Strange.

Not necessarily "normal procedure", just good practice.

To help debug your CSS image paths, get Firebug or some Developer tools up and see if the images being loaded by the CSS are throwing 404 errors. Also, if "/path" is the path on your server to your site, and your CSS is kept in "/path/css" and your images in "/path/images", you should be referencing them as


url('../images/image.png');

Just for good measure, try referencing them as


url('/site/images/image.png');

just to make sure we’re not missing anything.

That actually should work, if your css directory is at the same level as the images directory.

It just means ‘one up and then images’ - it’s not cached?

It’s two dots and a slash.

Try and do a Ctrl+F5 - you should see the images. Unless you’ve spelled it wrong.

Your CSS scripts are picked up and used?

You are right. I forgot to refresh.

This works for image src’s:


<img src="/images/logo.png">

and


<img src="../images/logo.png">

And this relative path works for CSS urls:


url('../images/logo.png')

Thanks all for the help. Much appreciated.