Welcome Guest, Not a member yet? Register   Sign In
End my urls with trailing slash (/) or not? And how do I go about it doing this..
#1

[eluser]rreynier[/eluser]
Hey guys, I am trying to figure out which is most appropriate. From the articles I have read, it seems best to end url's with a trailing slash.

So instead of:
http://www.site.com/article

It would read:
http://www.site.com/article/

First I adjusted my htaccess to force a trailing slash.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^.+[^/]$
RewriteRule ^(.+)$ $1/

Then I started implementing this in my links and I thought if I did anchor('article/','article') it would work, but it seems that this function strips the trailing slash.

To get around this I changed the config file to have $config['url_suffix'] = '/'. Which worked..MOSTLY fine.. except I have a document area on my site with pdf's and such. So the links created there would turn out like http://www.site.com/documents/doc1.pdf/ . This of course does not work.

What do you think my solution is here? I guess I could go back to any page I referenced documents or files in and adjust them to not use the anchor function, but I feel like there should be an easier way.

Thanks!
#2

[eluser]mjsilva[/eluser]
Hi rreynier,

Yesterday I've face the same problem, here is the solution I've found:

Create a MY_Config.php in your application/libraries and overwrite site_url function with:

Code:
/**
* Site URL
*
* @access    public
* @param    string    the URI string
* @return    string
*/
function site_url($uri = '')
{
    if (is_array($uri))
    {
        $uri = implode('/', $uri);
    }

    if ($uri == '')
    {
        return $this->slash_item('base_url').$this->item('index_page');
    }
    else
    {
        $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
        // check if link have a "file like" extension, if it have remove suffix
        $suffix = (preg_match('/.*\.[a-z]{3}$/', $uri)) ? '' : $suffix;
        return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
    }
}


That should make the job to prevent suffix for links to files.
#3

[eluser]mjsilva[/eluser]
Ups... you probably have it figured by now, only now I notice the post date is from March, anyway here's the solution for others who might face the same problem.
#4

[eluser]Eric Barnes[/eluser]
Just edit your config:
Code:
$config['url_suffix'] = "/";
#5

[eluser]mjsilva[/eluser]
The problem is if you have something like
Code:
<?=anchor('mypdffile.pdf', PDF)?>
it will become
Code:
<a href="http://yoursite.com/mypdffile.pdf/">PDF</a>
an invalid link.

Check my "hack" for the solution.
#6

[eluser]Eric Barnes[/eluser]
Yea but only if you have removed index.php. Otherwise it would become yoursite.com/index.php/myfile.pdf. If you release anything for the masses you should be using href="&lt;?=base_url()?&gt;myfile.pdf"

At any rate it is all about semantics and what you prefer. Smile




Theme © iAndrew 2016 - Forum software by © MyBB