End my urls with trailing slash (/) or not? And ho

2020-03-24 01:21发布

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!

3条回答
不美不萌又怎样
2楼-- · 2020-03-24 01:49

Sorry to maybe be rude-ish, but if it doesn't work (or causes problems), just don't do it and you're fine. Adding extra code for that kind of thing is wasted time. Some browsers add the slash automatically, so why worry? It really does not matter.

查看更多
叼着烟拽天下
3楼-- · 2020-03-24 01:51

My sites use both. The trailing slash indicates something specific to users, i think. Since the "/" usually indicates a folder, users (especially technical) take this to mean that there are sub-folders or sub-parameters to this page. Not showing a user that slash could indicate that there are no sub folders. This works for things like files, such as your pdf file.

Because of this, I have chosen not to force either side, but to build my site in a way that reflects the users perceptions. But thats a philosophy thing, you build a site for users, not for the code, right? :-)

查看更多
▲ chillily
4楼-- · 2020-03-24 01:53

Override site_url() from Config.php to add a trailing slash, except when linking to a file:

application/core/MY_Config.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Config extends CI_Config {

    function site_url($uri = '')
    {   
        if ($uri == '')
        {
            return $this->slash_item('base_url').$this->item('index_page');
        }

        if ($this->item('enable_query_strings') == FALSE)
        {
            $suffix = '';
            if( ! preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
            {
                $suffix = '/';              
            }
            return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;              
        }
        else
        {
            return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
        }
    }

}
// END MY_Config Class

/* End of file MY_Config.php */
/* Location: ./application/core/MY_Config.php */
查看更多
登录 后发表回答