cms icon indicating copy to clipboard operation
cms copied to clipboard

A site's root URL always has a trailing slash, regardless of `addTrailingSlashesToUrls`

Open benface opened this issue 5 years ago • 8 comments

Steps to reproduce

  1. Set the general config option addTrailingSlashesToUrls to false (or just don't set it, as it's the default)
  2. Write the following in a template: {{ siteUrl() }}
  3. Render that template from a site that has a base URL that ends with a path, like https://example.com/en

Expected result: https://example.com/en Actual result: https://example.com/en/

Additional info

  • Craft version: 3.4.5
  • PHP version: 7.3.13
  • Database driver & version: MySQL 5.5.5

benface avatar Feb 18 '20 19:02 benface

@benface This isn't Craft CMS's related solution but you could solve it with web server. I use NGINX and all trailing slashes at the end of URI that web server match rewrites and friendly 301 redirects it to URI with no trailing slash.

server {
    rewrite /(.*)/$ /$1 permanent;
}

Your actual result: https://example.com/en/ After using rewrite: https://example.com/en

As this minor bug is coming from Craft CMS than it'd solved.

dominikkrulak avatar Feb 25 '20 15:02 dominikkrulak

@dominikkrulak Thank you. Yes, I know and already do this. The issue is that following a link to the home page causes a 301 redirect (so two requests instead of one), since the original link from Craft contains a trailing slash, and the web server redirects to the no-trailing-slash URL.

benface avatar Feb 25 '20 15:02 benface

Unfortunately this is going to have to wait until 4.0, because it would end up breaking a lot of templates that currently do things like:

<link rel="stylesheet" type="text/css" href="{{ siteUrl }}assets/style.css">

That assumes that siteUrl will always end in a slash.

brandonkelly avatar Apr 01 '20 22:04 brandonkelly

Ah yeah, I see. No problem at all.

benface avatar Apr 01 '20 22:04 benface

Also, while this is definitely a bit awkward for site URLs that include a URI segment (e.g. http://example.com/en), the current behavior makes sense for base URLs without one (e.g. http://example.com/) – as browsers will end up redirecting to http://example.com/ even if you go to http://example.com (no slash). That’s not immediately obvious by looking at the address bar, but try going to github.com and outputting document.location.href in your browser console – it will output https://github.com/.

brandonkelly avatar Apr 01 '20 22:04 brandonkelly

If use SEOmatic and you're looking for a temporary work-around for this issue, this is how I solved it for our sites temporarily until the fix comes out:

{% set currentUrl = currentSite.handle == 'english' ? entry.url : entry.url|trim('/', 'right') %}
{% do seomatic.meta.canonicalUrl(currentUrl) %}

For our sites, this gives us:

  • http://www.notrealsite.com/
  • http://www.notrealsite.com/de
  • http://www.notrealsite.com/fr

It's definitely not hack-free, but it works nicely and it's only one extra line of code in my template.

mizziness avatar Jun 08 '20 15:06 mizziness