trailing slash что это
When should I use a trailing slash in my URL?
Is there a proper way of knowing which to use?
9 Answers 9
It is not a question of preference. /base and /base/ have different semantics. In many cases, the difference is unimportant. But it is important when there are relative URLs.
In my personal opinion trailing slashes are misused.
Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.
A typical URL for this book on a Unix-like operating system would be a file path such as file:///home/username/RomeoAndJuliet.pdf, identifying the electronic book saved in a file on a local hard disk.
Another good source to read: Wikipedia: URI Scheme
According to RFC 1738, which defined URLs in 1994, when resources contain references to other resources, they can use relative links to define the location of the second resource as if to say, «in the same place as this one except with the following relative path». It went on to say that such relative URLs are dependent on the original URL containing a hierarchical structure against which the relative link is based, and that the ftp, http, and file URL schemes are examples of some that can be considered hierarchical, with the components of the hierarchy being separated by «/».
That is the question we hear often. Onward to the answers! Historically, it’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file:
http://example.com/foo/ (with trailing slash, conventionally a directory)
http://example.com/foo (without trailing slash, conventionally a file)
A slash at the end of the URL makes the address look «pretty».
A URL without a slash at the end and without an extension looks somewhat «weird».
You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?
BUT I’m being a proponent of web best practices regardless of the environment. It can be wonky and unclear, just as you said about the URL with no ext.
Using these guidelines, it’s wrong to put a slash after a non-directory resource.
That’s not really a question of aesthetics, but indeed a technical difference. The directory thinking of it is totally correct and pretty much explaining everything. Let’s work it out:
You are back in the stone age now or only serve static pages
You have a fixed directory structure on your web server and only static files like images, html and so on — no server side scripts or whatsoever.
So far so good, this is the expected case. It already shows the difference in handling, so let’s get into it:
At 5:34am you made a mistake uploading your files
So, you delete that file and tell the guy to reload the page. Your server looks for the /dvd file, but it is gone. Most servers will then notice that there is a directory with that name and tell the client that what it was looking for is indeed somewhere else. The response will most likely be be:
Status Code:301 Moved Permanently with Location: http://[. ]/dvd/
Finally after receiving this response, the client loads /dvd/ and everything is fine.
«Just fine» is not good enough for you
You have some dynamic page where everything is passed to /index.php and gets processed. Everything worked quite good until now, but that entire thing starts to feel slower and you investigate.
Summary:
If it ends with / it can never be a file. No server guessing.
Trailing Slashes
Register for the Ryte Newsletter
Get the latest SEO and website quality news! Exclusive content and Ryte news delivered to your inbox, every month.
A trailing slash is the forward slash placed at the end of a URL. The trailing slash is generally used to mark a directory, and if a URL is not terminated using a trailing slash, this generally points to a file. However, these are guidelines, and not requirements. The process of setting a single trailing slash for directories has established itself as a standard over time. The slashes play an important role in search engine optimization, especially in the area of duplicate content.
Contents
Trailing slashes and SEO [ edit ]
Although the trailing slash in a URL does not contain concrete information, its importance in search engine optimization cannot be ignored. This should be taken into account mostly in cases where websites can be accessed through a URL with a different number of trailing slashes, such as a page that can be opened with two or no trailing slashes. This results in duplicate content which, according to Google, should be avoided. This is because individual content should only be accessible through a single URL.
This multiple indexing can negatively effect the website in ways other than creating duplicate content, which causes confusion for Google. If several URLs are indexed, the incoming traffic is divided into different placements in the SERPs. It is therefore difficult for the different domain variants to establish themselves in the search engine rankings. Therefore, there may be several URLs in the search engine index, but none of these URLs rank well because the search engine has to choose between them.
Therefore, it is preferable to choose one option: either with or without a trailing slash. This is the only way to prevent unwanted duplicate content from entering the index.
Avoiding duplicate content [ edit ]
To ensure the content of a website does not appear multiple times in the index through a modified URL, you should use a 301 redirect to redirect between domains. This way, incoming traffic can be re-bundled and directed to the actual and only target page. Thus, the URL duplicated using trailing slashes loses its validity.
Removing trailing slashes with the re-write rule [ edit ]
Significance in practice [ edit ]
Incorrectly set trailing slashes may cause search engines to ignore the appropriate indication when using canonical tags. For this reason, the URL specified in the Canonical tag must also contain the trailing slash, if one exists. The same applies to the country identification of URLs using hreflang or to the forwarding of URLs via 301 redirect. If there are no slashes in these cases, the country assignment can fail or pages are not redirected correctly.
Trailing slash что это
Laravel URL Route Trailing Slash
This extension allows enforcing URL routes with or without trailing slash.
For license information check the LICENSE-file.
The preferred way to install this extension is through composer.
to the require section of your composer.json.
Once package is installed you should manually register \Illuminatech\UrlTrailingSlash\RoutingServiceProvider instance at your application in the way it comes before kernel instantiation, e.g. at the application bootstrap stage. This can be done in ‘bootstrap/app.php’ file of regular Laravel application. For example:
Note: \Illuminatech\UrlTrailingSlash\RoutingServiceProvider can not be registered in normal way or be automatically discovered by Laravel, since it alters the router, which is bound to the HTTP kernel instance at constructor level.
In order to setup automatic redirection for the routes with trailing slash add \Illuminatech\UrlTrailingSlash\Middleware\RedirectTrailingSlash middleware to your HTTP kernel. For example:
This extension allows enforcing URL routes with or without trailing slash. You can decide per each route, whether its URL should have a trailing slash or not, simply adding or removing slash symbol (‘/’) in particular route definition.
Tip: the best SEO practice is having trailing slash at the URLs, which have nested pages, e.g. «defines a folder», and have no trailing slashes at the URLs without nested pages, e.g. «pathname of the file».
Heads up! Remember, that with this extension installed, you are controlling requirements of URL trailing slashes presence or absence in each route you define. While normally Laravel strips any trailing slashes from route URL automatically, this extension gives them meaning. You should carefully examine your routes definitions, ensuring you do not set trailing slash for the wrong ones.
You’ll have to deal with trailing slash for root URL separately at the server settings level.
You can define trailing slash presence for resource URLs using the same notation as for regular routes. In case resource name is specified with trailing slash, all its URLs will have it. For example:
You can control trailing slash presence per each resource route using options ‘trailingSlashOnly’ and ‘trailingSlashExcept’ options. These ones behave in similar to regular ‘only’ and ‘except’, specifying list of resource controller methods, which should or should not have a trailing slash in their URL. For example:
Note: ‘trailingSlashExcept’ option takes precedence over ‘trailingSlashOnly’.
Trailing Slash in Unit Tests
Since Illuminatech\UrlTrailingSlash\RoutingServiceProvider can not be registered as regular data provider, while writing unit and feature tests you will have to manually register it within test application before test kernel instantiation. This can be done within your \Tests\CreatesApplication trait:
However, this in not enough to make tests running correctly, because Laravel automatically strips trailing slashes from requests URL before staring test HTTP request. Thus you will need to override \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::prepareUrlForRequest() in the way it respects trailing slashes. This can be achieved using Illuminatech\UrlTrailingSlash\Testing\AllowsUrlTrailingSlash trait. For example:
Clarification on trailing slashes, urls in sitemaps and canonical urls? #27889
I am currently working on a gatsby website and I’ve been struggling to find the «best practise» and implementation for all things related to trailing slashes:
After researching, it seems other people are confused as well and I don’t feel that the documentation is clear as to what the best practices are and how to go about achieving them. It seems there is an unnecessary amount of out-of-the box learning when it comes to best setup. Also, which bits require server configuration and which bits do not.
Here are my questions:
If the resource you are linking to is a directory then it should have a trailing slash. As Gatsby, by default creates directories for URLs, then they should include the trailiing slash?
2. Should users setup 301 redirects from no trailing slash to trailing slash?
Accordinig to the previous links, the non-trailing slash URL should have a 301 redirect to the trailinig slash URL.
When setting up, it seemed like the plugin gatsby-plugin-force-trailing-slashes would do this for you however it doesn’t do the 301 redirect which is a server setup problem.
In this project, I am using Cloudfront, so used this function to redirect from no trailing slash to trailiing slash. This is not a trivial thing for someone starting out to work out?
I know on Netlify there are other options to do tthis.
Note: Alternatively gatsby-plugin-create-page-html is a plugin that creates an about.html file from an about/index.html file. However, this just means you have two resources for the same content, which I believe isn’t good.
3. Your pages should include a canoncial URL that includes the trailing space
I tried both gatsby-plugin-canonical-urls and gatsby-plugin-react-helmet-canonical-urls to add canonic URLs to my pages. In the documentation for gatsby-plugin-canonical-urls it says:
With the above configuration, the plugin will add to the head of every HTML page a rel=canonical e.g.
This show that the URL should have a trailing slash.
FYI: I added patch for gatsby-plugin-react-hemet-canonical-urls to force an option to force a trailing slash:
I know these are more of a collection of thoughts but I feel like there are lots of people asking questions related to these issues so the documentation could perhaps be more direct at addressing the solutions?
Replies
Hi, thanks for the issue!
What documentation did you find confusing? Can you directly link to it and highlight the portions of it?
Gatsby by default creates paths with a trailing slash, everything after that is your personal preference and I don’t think we make any recommendations or comments about this. Moreover the documentation should be specific to Gatsby so I don’t see the need for explanations about server redirects or SEO implications as there are enough articles on the internet already.
The example you cited from the canonical URL plugin just happens to have the trailing slash as that’s what Gatsby is creating by default. It doesn’t imply that canonical URLs have to have a trailing slash.
I’d recommend sticking to one of the two options (non-trailing/trailing) and keeping it consistent everywhere.
AleksandrHovhannisyan Dec 22, 2020
Hmm, I’m working on migrating my site to Gatsby, and locally, I see both trailing and non-trailing variants for all pages. How do I enforce one or the other? I’m on Gatsby v2.26.1.
EDIT April 2021: This post ended up helping a lot of folks so do feel free to give it a read, but if you want to know more and explore the why/how behind the workflow, I explored this further on my blog: https://jonsully.net/blog/trailing-slashes-and-gatsby/
Here’s what we know for certain:
Reach Router made a choice to ignore trailing slashes completely, therefore eliminating any difference when navigating up the path.
So how to we reconcile all of that? Here’s what my opinion would be this. Step 0 is to make the decision to unify your site with or without the trailing slash. The goal is to have uniformity and non-duplicated content, that’s all. Gatsby generates static files in preparation for a trailing slash. I don’t like fighting my tools. My vote is go ahead and use the trailing slash. So, to do that:
First
Make sure your web server of choice is following standard web-server parlance.
Sidenote: I’m a Netlify guy and Netlify’s platform supports a feature called Pretty URLs, which enforces all of the above for you. Make sure it’s enabled (it is by default, but make some cURL requests to your site to double-ensure). See here for more info.
Second
This + the next step will also prevent the odd ‘trailing-slash-flash’ that can occur when you reload a page that did / didn’t have a trailing slash and it flips to the other way.
Third
That’s it. If you can nail those three steps, your site should be exclusively running in a trailing-slash-only, SEO-friendly, link-sharing-friendly format. Deploy it, click around, refresh to your heart’s content, hit it from cURL; all of your pathing should be uniform.
everything after that is your personal preference and I don’t think we make any recommendations or comments about this
That’s true, but it doesn’t necessarily leave Gatsby users feeling the most satisfied. Gatsby can work without trailing slashes and with.. but you have to know the layers (including the server layer) well to set that up. I hope this provides some basis for how to do that 🙂
trailing slash
Смотреть что такое «trailing slash» в других словарях:
Persistent Uniform Resource Locator — A persistent uniform resource locator (PURL) is a Uniform Resource Locator (URL) (i.e. location based Uniform Resource Identifier or URI) that does not directly describe the location of the resource to be retrieved but instead describes an… … Wikipedia
cp (Unix) — cp is a UNIX command used to copy a file. Files can be copied either to the same directory or to a completely different directory, possibly on a different file system or hard disk drive. If the file is copied to the same directory, the new file… … Wikipedia
HTML element — This article is about the HTML elements in general. For information on how to format Wikipedia entries, see Help:Wiki markup and Help:HTML in wikitext HTML HTML and HTML5 Dynamic HTML XHTML XHTML Mobile Profile and C HTML Canvas element Character … Wikipedia
URL normalization — (or URL canonicalization) is the process by which URLs are modified and standardized in a consistent manner. The goal of the normalization process is to transform a URL into a normalized or canonical URL so it is possible to determine if two… … Wikipedia
History of BMW motorcycles — BMW s motorcycle history began in 1921 when BMW commenced manufacturing engines for other companies. The motorcycle subsidiary now operates under the BMW Motorrad division. BMW (Bayerische Motoren Werke AG) introduced the first motorcycle under… … Wikipedia
Wikipedia:Manual of Style — This guideline is a part of the English Wikipedia s Manual of Style. Use common sense in applying it; it will have occasional exceptions. Please ensure that any edits to this page reflect consensus. Shortcuts … Wikipedia
Backslash — The backslash ( ) is a typographical mark (glyph) used chiefly in computing. It was first introduced to computers in 1960 by Bob Bemer. [ [http://www.thocp.net/biographies/bemer bob.htm Mini Biography of Bob Bemer] ] Sometimes called a reverse… … Wikipedia
Mapping of Unicode characters — Unicode’s Universal Character Set has a potential capacity to support over 1 million characters. Each UCS character is mapped to a code point which is an integer between 0 and 1,114,111 used to represent each character within the internal logic… … Wikipedia
Economic Affairs — ▪ 2006 Introduction In 2005 rising U.S. deficits, tight monetary policies, and higher oil prices triggered by hurricane damage in the Gulf of Mexico were moderating influences on the world economy and on U.S. stock markets, but some other… … Universalium
Date and time notation by country — Different style conventions and habits exist around the world for dates and times in writing and speaking. Examples:*The order that a year, month, and day are written. *How weeks are identified. *The 24 hour clock and/or the 12 hour clock. *The… … Wikipedia