MyBB Community Forums

Full Version: Relative URLs & sub-directories
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
All of the user-accessible files are in the root directory, so MyBB uses relative URLs everywhere. I don't like this (and I don't think it's the best idea) so is there a way of switching this?

I'd like to do something like this: Without things breaking.

One issue is that uploaded avatars are given relative URLs, so I can't simply add $mybb->settings['bburl'] to the start of the HTML image source, cause it'll add them for linked avatars too.

Any ideas?
Hi Jazza,
This part:
The above will not break anything*, assuming 'chat' and 'news' are external scripts, not MyBB plugins.
*Nothing will be "broken" or integrated either. Toungue


#
One issue is that uploaded avatars...
Are you saying you want to use MyBB avatars in an "external" script in the "news" folder?

Please, make your exact questions more clear, and I'm sure people will try to help...
Thanks for the response.

The "chat" and "news" directories use a common trick that when you visit a directory, it'll default to index.php if no file is specified. These index.php scripts are just custom MyBB pages that use the MyBB header, footer etc., templates. These templates aren't designed to be used in nested directories, so URLs sometimes break (like below).



Uploaded avatars are stored in the database like...

/avatars/user523.jpg

Linked avatars are stored like...

http://www.externalwebsite.com/images/avatar.jpg

Which means uploaded avatars will only work in root directory files. If you try to use the uploaded avatar in a file like /news/archive.php, it'll try and look for the image in /news/avatars/ and wont find it.

My first attempt to fix that was to add $mybb->settings['bburl'] to the image src, but that means externally linked files also have the BBURL value at the start like:
http://www.myforum.com/forum/http://www....avatar.jpg

I use user avatars on every page in the navigation (like a "you are signed in as" ... thing) so when one of my users visits a nested file, their avatar breaks.
You can add a <base> tag to your headerinclude and then hope for the best.

It's a suboptimal solution though; for a proper solution you'd have to change a lot of code in MyBB.
(2012-06-26, 07:55 AM)frostschutz Wrote: [ -> ]You can add a <base> tag to your headerinclude and then hope for the best.

It's a suboptimal solution though; for a proper solution you'd have to change a lot of code in MyBB.

Urgh. I'll see about tweaking the .htaccess file to rewrite URLs.

Well I fixed one thing. I wrote a small function that checks if you have a relative URL as your avatar and if so, adds a domain name to the start to make it absolute.

function fix_user_avatars() {
	global $mybb;
	
	$src = $mybb->user['avatar'];
	$domain = 'http://www.yourboardurl.com'; //Use the bburl setting?
	
  if (parse_url($src, PHP_URL_SCHEME) != '') {
    $newsrc = $src;
  } else if ($src[0] == '#' || $src[0] == '?') {
  	$newsrc = $base.$src;
  } else {
  	//Dump the array values into variables...
	  extract(parse_url($domain));
	  
	  $abs = ($src[0] == '/' ? '' : preg_replace('#/[^/]*$#', '', $path))."/$src";
	  
	  $re  = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
	
	  for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n));
	  
	  //Construct the new URL...
	  $newsrc = $scheme.'://'.$host.str_replace('../', '', $abs);
  }
  
  //Overwrite the user's avatar with a now absolute URL...
  $mybb->user['avatar'] = $newsrc;
}