Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
benface opened this issue Feb 18, 2020 · 10 comments
Labels
bug severity:minor Cosmetic issues or bugs with simple workarounds

Comments

@benface
Copy link
Contributor

benface commented Feb 18, 2020

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
@brandonkelly brandonkelly added bug severity:minor Cosmetic issues or bugs with simple workarounds labels Feb 18, 2020
@brandonkelly brandonkelly added this to the 3.5 milestone Feb 18, 2020
@dominikkrulak
Copy link

dominikkrulak commented Feb 25, 2020

@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.

@benface
Copy link
Contributor Author

benface commented Feb 25, 2020

@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.

@brandonkelly brandonkelly modified the milestones: 3.5, 4.0 Apr 1, 2020
@brandonkelly
Copy link
Member

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.

@benface
Copy link
Contributor Author

benface commented Apr 1, 2020

Ah yeah, I see. No problem at all.

@brandonkelly
Copy link
Member

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/.

@mizziness
Copy link

mizziness commented Jun 8, 2020

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:

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

@WHITE-developer
Copy link

Very old issue, but it's still there. Any update on this?

@brandonkelly
Copy link
Member

Sorry, looks like this fell through the cracks. Just added it to our v6 list. In the meantime, the previous workaround will still do the trick.

@tibbis
Copy link

tibbis commented Dec 25, 2024

Noticed this issue on my end as well - what is the temporary solution for alternate and sitemaps urls (seomatic) etc? Affects single home page entry url with subdirectory like example.com/de.

khalwat added a commit to nystudio107/craft-seomatic that referenced this issue Dec 28, 2024
…prefix strip trailing slashes as appropriate ([#717](#717)) ([#5675](craftcms/cms#5675))
khalwat added a commit to nystudio107/craft-seomatic that referenced this issue Dec 28, 2024
…prefix strip trailing slashes as appropriate ([#717](#717)) ([#5675](craftcms/cms#5675))
khalwat added a commit to nystudio107/craft-seomatic that referenced this issue Dec 28, 2024
…prefix strip trailing slashes as appropriate ([#717](#717)) ([#5675](craftcms/cms#5675))
@khalwat
Copy link
Contributor

khalwat commented Dec 28, 2024

While I think this is largely as non-issue, and will not affect SEO in any measurable way, I've implemented it in SEOmatic.

Craft CMS 3:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-seomatic": "dev-develop as 3.5.10”,

Then do a composer clear-cache && composer update

…..

Craft CMS 4:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-seomatic": "dev-develop-v4 as 4.1.9”,

Then do a composer clear-cache && composer update

…..

Craft CMS 5:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-seomatic": "dev-develop-v5 as 5.1.9”,

Then do a composer clear-cache && composer update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug severity:minor Cosmetic issues or bugs with simple workarounds
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants