Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I save a path with or without a trailing slash at the end, what's the convention

I'm always confuse whether I should add a trailing slash at the end of a path, and often mix it up, leading to some file no found. Example with drupal:

$base_theme = '/sites/all/themes/myTheme/';

or

$base_theme = '/sites/all/themes/myTheme';

The image path could extend the base theme:

$base_image = $base_theme.'images/';

or

$base_image = $base_theme.'/images';

Is there any convention? Or I can pick which one I prefer?

I would choose to finish all path with a trailing slash since too many slash is better than no slash.

like image 956
Jonathan de M. Avatar asked Jul 02 '13 07:07

Jonathan de M.


People also ask

Should you have a trailing slash?

Historically, a trailing slash marked a directory and a URL without a trailing slash at the end used to mean that the URL was a file. Today, however, trailing slashes are purely conventional, and Google does not care whether you use them; as long as you're consistent.

What happens if you skip the trailing slash?

When using APPEND_SLASH , if they accidently sent it without trailing slash, and your urlconf is WITH a trailing slash they would get an exception about data lose when redirecting POST requests.

Should REST endpoints end with slash?

REST API's should not expect a trailing slash and should not include them in the links that they provide to clients. However, every character within a URI counts toward a resource's unique identity. Two different URIs map to two different resources.

What is the name of the path which is ended with a slash?

"If it ends with a slash, it's a directory.


1 Answers

TL;DR: There's no real convention. Trailing slash would be the more globally easy to recognize format. The important thing is that you're consistent through your design and that you convey your usage clearly.


There's no real convention; but there are considerations to make.

Advantages in trailing slash:

  1. Trailing slash usually indicates a folder path (or a prettified URL) whereas a file extension denotes a direct file link. (Think example.com/home/ VS example.com/style.css).
  2. This is usually friendlier for people coming from UNIX and such, as in the terminal a clear convention is to leave a trailing slash for directories.
  3. As a programmer - adding a trailing slash will result in less-likely programmer errors; for example: accidentally adding a second slash will look ugly (http://example.com/styles//myfile.css) but will not break the file link. Forgetting a slash will: http://example.com/stylesmyfile.css, however the behavior might be confusing for query strings: http://example.com/thread?id=1 VS http://example.com/thread/?id=1 <- the result really depends on how you handle your .htaccess.

Advantages in no trail:

  1. Prettier, some might say
  2. It's easier to remember and it's more readable to always add a slash when appending paths to a variable string than not. i.e. it's easier to remember $baseURL . '/path.php' than $baseURL . 'path.php'
like image 171
casraf Avatar answered Oct 17 '22 10:10

casraf