What is the best way to truncate a URL when displaying it within a web page? I don't mean a link but literally displaying the URL as a value to the user, assuming that the text might be in a container of fixed width and you don't want to wrap or run outside of the container?
Is it better to truncate from the end, favouring the early part of the url:
eg. http/really.long/urlthaticantf...ere.html
Or place the '...' in the middle to favour the start and end of the link as the most value in terms of giving context:
eg. http/really.long/ur...aticantfithere.html
Also, what is a good rule of thumb when choosing how long to make the truncated URL? Should you be pessimistic and pick a likely wide-character such as capital 'M' and see how many of these fit in the layout? This tends to give really short URLs in general as most characters are much narrower than 'M'.
Or should you be optimistic and use a truncation that generally gives a good length but risk overrunning when the URL contains many large characters?
There are several things that you can do to avoid URLs that are too long: If using dynamic URLs with URL parameters, use server-side URL rewrites to convert them into static, human-readable URLs. Try to minimize the number of parameters in the URL whenever possible.
The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems. The <br> tag is an empty tag which means that it has no end tag.
URLs should not use underscores, spaces, or any other characters to separate words. Use lowercase letters. In some cases, uppercase letters can cause issues with duplicate pages. For example, moz.com/Blog and moz.com/blog might be seen as two distinct URLs, which might create issues with duplicate content.
My preference is to display the most critical components of the URL. This is the file being requested and the domain of the request are what I consider critical, the intermediate path and the query string are things I consider non-critical.
So if you had http://www.Example.com/archives/2005/08/09/something.html, I would truncate it as www.Example.com/.../something.html
Of course, there are scenarios where this wont work. Take the URL of this page:
What's the best way to truncate a URL so that it fits within a layout
In this case, I would truncate the last portion of the URL to a reasonable number of characters (preferably breaking on a non-alpha), such as:
stackoverflow.com/.../whats-the-best...
Truncate the middle, for the reasons you gave.
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