Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a directory path variable end with a trailing slash?

When defining a path to a directory as a variable or constant, should it end with a trailing slash? What is the convention?

pwd in Unix shows your current directory without a trailing slash, while the tab complete of cd /var/www/apps/ includes the trailing slash, which left me unsure.

like image 342
meleyal Avatar asked Jun 11 '09 09:06

meleyal


People also ask

Should routes have trailing slash?

The short answer is that the trailing slash does not matter for your root domain or subdomain. Google sees the two as equivalent. But trailing slashes do matter for everything else because Google sees the two versions (one with a trailing slash and one without) as being different URLs.

Does trailing slash matter SEO?

A trailing slash at the end of a URL on your website can cause issues with duplicate content if not dealt with correctly. Put simply, Google doesn't like seeing the same content on different pages. It can be confusing for both search engines and users.

What is a slash directory?

Slash is the path separator in Linux. We use it to separate the current directory (.) from the file name. Without it, we have .

What are trailing slashes?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.


1 Answers

I go with the trailing slash because:

  1. "If it ends with a slash, it's a directory. If not, it's a file." is an easy convention to remember.

  2. At least on the operating systems I commonly use, doubling the slash causes no problems, while omitting the slash causes big ones. It is, therefore, safest to both put the slash into the variable and use "$path/$file" when making use of it.

like image 125
Dave Sherohman Avatar answered Oct 06 '22 13:10

Dave Sherohman