ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Relative URLs, or Absolute URLs?

January 16, 2008 8:35pm

Subscribe [6]
  • #1 / Jan 16, 2008 8:35pm

    mdavis1982

    46 posts

    Hi all…

    I’ve been working with CodeIgniter for a while now, and was wondering about something that I haven’t really thought about before. In my view files Ive been doing this:

    <?php echo base_url(); ?>public/images/branding/logo.jpg

    This generates an absolute URL. What I am wondering is if there is a better way to do this so that the urls generated are relative urls to save on HTTP requests to make my site faster.

    Any advice would be greatly appreciated!

    Thanks…

    Matt

  • #2 / Jan 16, 2008 8:43pm

    Michael Wales

    2070 posts

    I would make a helper function that echos a relative (from root) URL to the image. Then when you go from development to production you only have to change that one function (rather than every image throughout the entire site).

  • #3 / Jan 16, 2008 8:46pm

    mdavis1982

    46 posts

    I’m not sure I understand what you mean. Is there any chance you could show me an example, please? It would be very much appreciated!

    Cheers…

    Matt

  • #4 / Jan 16, 2008 11:19pm

    wiredesignz

    2882 posts

    relative URL’s don’t make a page load faster or lower server load, your browser simply adds the current domain to the request.

  • #5 / Jan 18, 2008 6:18pm

    Josh Giese

    26 posts

    I think he is talking about the page weight.

    “http://www.somedomain.com/images/picture.jpg”
    is heavier then
    “/images/picture.jpg”

    personally, I wouldn’t really view it as a problem. most of your visitors wont feel the difference.

  • #6 / Jan 18, 2008 6:29pm

    wiredesignz

    2882 posts

    Better to use BASE HREF in the header with relative urls:

    <BASE HREF=“http://domain.com/etc”>

  • #7 / Jan 18, 2008 7:31pm

    mdavis1982

    46 posts

    Josh, that’s exactly what I was talking about…

    I just think there should be some easy way in CI to generate relative URLs for images, etc.

    Thanks all… Any more ideas would be greatly appreciated.

    Matt

  • #8 / Jan 18, 2008 10:46pm

    wiredesignz

    2882 posts

    application/helpers/asset_helper.php

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    
        function asset($type = 'css', $source = NULL, $attrib = NULL, $relativeURL = TRUE)
        {
            $href = ($relativeURL) ? '' : base_url();
            
            $source = $href . str_replace(".{$type}", '', $source);
            
            switch ($type)
            {            
                case 'css': 
                {
                    return '<link rel="stylesheet" type="text/css" href="'.$source.'.'.$type.'" '.$attrib.' />'."\n";
                }
                case 'js':
                {
                    return ''."\n";
                }
                case 'img':
                {
                    return ''.$source.'."\n";
                }
            } 
        }

    Useage in View

    <?php echo asset('css', 'css/stylesheet', 'media="screen"'); //relative URL ?>
    
    <?php echo asset('img', 'images/logo.jpg', 'alt="logo"', FALSE); // full URL ?>


    The forum killed the javascript part of the helper? I think you get the idea though.

  • #9 / Jan 19, 2008 9:49am

    Phil Sturgeon

    2889 posts

    Yea the script tag is stripped by the XSS cleaner, so asset helpers always get broken in forums :p

    Check out my asset helper for a modular sorting system too.

  • #10 / Jan 19, 2008 11:31am

    Sarre

    52 posts

    The base_url() function just outputs the config value in config.php, so if you have a relative url in there, like

    $config['base_url'] = "/some/relative_url/";

    the base_url() function will output that relative url value…

    What else do you need?

    (Or am I mistaken? :p)

  • #11 / Jan 19, 2008 2:05pm

    Phil Sturgeon

    2889 posts

    Not a great plan to overwrite the base_url method. Thats gonna have a bunch of nasty effects that you really dont want to deal with.

    Why not just add your own function to the url helper and make base_path() or just make your own config variable thats autoloaded. Much easier to add a new method than change an important part of the framework.

  • #12 / Feb 08, 2010 8:45am

    jayapalchandran

    20 posts

    Yeah, as stated by the previous reply….
    $config[‘rel_url’] = ‘/folder/’ // if you are running from localhost and just ‘/’ for the server but still if you are running from a sub folder then ‘/folder/’ would be the relative url and add a function like the samd as function base_url for rel_url like function rel_url()

    if ( ! function_exists('base_url'))
    {
        function base_url()
        {
            $CI =& get_instance();
            return $CI->config->slash_item('base_url');
        }
    }
    
    if ( ! function_exists('rel_url'))
    {
        function rel_url()
        {
            $CI =& get_instance();
            return $CI->config->slash_item('rel_url');
        }
    }

    in the url_helper.php file…


    BUT…

  • #13 / Feb 08, 2010 8:49am

    jayapalchandran

    20 posts

    BUT…
    as i have been reading the complete page i found that a person has stated that there will be difference between accessing a url as relative and absolute from the same site.

    like images/logo.gif and sitename.com/images/logo.gif

    my understanding was that what ever it is the browser has to create a connection to the server to fetch that file using threads so that images show up asynchronously…

    may be instead of creating a new socket every time the browser can just open one connection and can send multiple requests with the one open connection if the request is relative

    and if the request is absolute then for each abs url the browser has to create a socket and that adds up to overhead…

    This is my idea and i request users to post their thought so that every one could have a clear idea.

  • #14 / Feb 08, 2010 8:54am

    jayapalchandran

    20 posts

    And i faced another problem by using absolute url is with flash and ajax.
    and ajax request with www and non www.
    if you have set baseurl as http://sitename.com and if you are accessing scripts using ajax then you might face this problem

    consider that the users is accessing the page using http://sitename.com and in your config you have did it like baseurl = ‘http://sitename.com . then there wont be a problem because there is no www in the browser url and also in the ajax request…

    but if users try to access the page as http://www.sitename.com then the absolute url for ajax will not work and it will throw Permission Denied error because the actual url is http://www.sitename and the now ajax url is http://site which are different and hence an error is thrown.

    the same error will throw up for flash or other controls i hope.
    so even in flash if you are displaying images using dynamically generated xml then you should access the url as relative and not absolute… so /folder/xmloutput.php would be appropriate…

  • #15 / Feb 08, 2010 8:59am

    jayapalchandran

    20 posts

    to circumvent these things better redirect non www to www using htaccess or redirect www to non www urls…

    like the following and use absolute urls…

    RewriteCond %{HTTP_HOST} ^www.site.com [NC]
    RewriteRule ^(.*)$ http:/site.com/$1 [L,R=301]

    my example is removing www from url.
    if you are hosting in a subfolder then add it to the rule.

    and BTW anytime anybody if there is objection then please post it so that all can update their mind if there is any corrections.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases