Should A URL end with a slash?
- It doesn’t matter if the domain name is followed by a slash
- Trailing slashes are important for other urls
- Files should not end with a slash
- Slash and SEO
-
- Trailing slash and non-trailing slash urls display the same content
- Trailing slash and non-trailing slash urls display different content
- Hreflang
- Add or remove trailing slashes
-
- .htaccess
- WordPress
- JavaScript framework
- The impact of slashes on reporting
A trailing slash is a forward slash (“/”) placed at the end of a URL, such as domain.com/ or domain.com/page/. Trailing slashes are commonly used to distinguish directories with trailing slashes from files without. However, these are guidelines, not requirements.
In the past, folders would have slashes, and files would not. The folder will indicate that there are more files, and you will usually have an index file (index.html, index.php, etc.) from which the page content will be loaded. Therefore, the content will come from domain.com/page/index.html, but domain.com/page/ will be displayed to the user. For a single file, you will have the filename and no trailing slash.
Urls on most systems today do not point to files. The URL is the record stored in the database. Serverless systems don’t even host files on your server.
Different URL structures may differ. Whether or not you choose to use a slash is a matter of personal preference, not anything else. Let’s look at some common scenarios.
It doesn’t matter if the domain name is followed by a slash
domain.com = domain.com/
Copy the code
These urls are treated exactly the same, and it doesn’t matter which version you use.
Trailing slashes are important for other urls
Domain.com/page indicates domain.com/page/Copy the code
In all cases except the slash immediately following the root field, the slash is treated as a separate URL.
Files should not end with a slash
In most cases, if you add slashes to.html,.php,.js,.css,.pdf,.jpg, etc., the file will not be loaded. This is because most systems will assume that the file is a folder, and since there is nothing behind this path, a 404 page or an error like the one below is usually returned.
Now, let’s look at the impact on SEO.
Slash and SEO
You may have to make different decisions depending on how the system works. These are some common situations that you might encounter.
Trailing slash and non-trailing slash urls display the same content
As mentioned earlier, if your content is visible in both the trailing slash and non-trailing slash versions of the page, you can treat the page as a separate URL. The common concern here is that content on different versions will lead to duplicate content. In most cases, this should not be a problem because the specification tag might specify the preferred version. Even without this information, Google will usually choose a preferred version for you to integrate signals. You can force the URL to be the preferred version as needed.
Whether you decide to use slashes or not, you want to make sure that all the different normalization signals (such as redirects, site maps, internal links, specification markers, etc.) point to the version to be indexed.
Trailing slash and non-trailing slash urls display different content
In some cases, if two systems share the same folder structure or use some A/B testing software, you may encounter situations where versions of urls with and without trailing slashes display completely different content. In these cases, ideally, you want to select a version to index and display to the user, and then redirect another version to that version.
Hreflang
You may encounter more complex setup issues involving Hreflang. The Hreflang link should point to the index version of the page. If the specification tags point to a version of the page with a trailing slash, and Google indexes the page that way, but the Hreflang tags point to a version of the page without a trailing slash, then these Hreflang tags may not be respected.
Add or remove trailing slashes
This process will change depending on your system. It is a good idea to check the documentation before making any changes.
.htaccess
Delete slash:
RewriteCond % {REQUEST_FILENAME}! -d RewriteRule ^ (. *) / $/ $1[L, R =301]
Copy the code
Pay attention to. ! -d searches for directories. If one exists, slashes are not deleted. If you do not include this option, you may break these home directory pages. Add slash:
RewriteCond % {REQUEST_FILENAME}! -f RewriteRule ^ (. * [^ /]) $/ $1 / [L, R = 301] ! -f Searches for a file. If the file exists, no slash is added. This prevents images, PDFS, JS, CSS, etc.
WordPress
If you go to Settings > Permalinks, you can change whether to use a custom structure to use slashes.
/%postname%/ will add a slash to the URL
/%postname%
The slash is removed from the url
JavaScript framework
These systems may be different from what you’re used to because of their routers. You can customize how urls work in your router, or – if you don’t want to spend too much time with your router – most of these systems have pre-built modules to add or remove trailing slashes.
The impact of slashes on reporting
Consider the report when deciding whether to use a slash. For example, in the Google Search Console, you can set domain or URL prefix properties. If you don’t add a slash when setting URL prefix properties (such as domain/folder), Google will add one anyway. As a result, all visits to domain.com/folder (excluding end-of-garbage) will not be reported because domain.com/folder/ (with a slash) has a higher level.
Google Analytics (GA) has the same problem trying to do in-depth analysis of content through folders if your main page doesn’t have slashes. If both the slash and no-slash versions of your url are applicable, both may be reported in the GA.
If you want to merge data, you can do as followsAdding filtersTo enforce a slash on the URL in the analysis report.
This is the regular expression: ^(/[a-z0 — 9/_-]*[^/])$
Last thought changes are always risky, so I wouldn’t force a change to your URL unless your setup is causing problems. Technology has changed, and the old URL convention of the slash doesn’t work for most modern sites.