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.
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.
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.
Slash is the path separator in Linux. We use it to separate the current directory (.) from the file name. Without it, we have .
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.
I go with the trailing slash because:
"If it ends with a slash, it's a directory. If not, it's a file." is an easy convention to remember.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With