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

Trailing slash issues #3127

Closed
NicoHood opened this issue Jan 5, 2021 · 15 comments
Closed

Trailing slash issues #3127

NicoHood opened this issue Jan 5, 2021 · 15 comments

Comments

@NicoHood
Copy link
Contributor

NicoHood commented Jan 5, 2021

There are multiple issues with trailing slashes in Grav.

The current situation:

  • https://mywebsite.de/contact is the canonial url
  • https://mywebsite.de/contact/ redirects to https://mywebsite.de/contact with 302 status code
  • The homepage is always https://mywebsite.de/, not https://mywebsite.de due to the http protocol. Browsers do not display them though!

Those are the current issues:

  1. Redirects should be 301 rather than 302 (Update: Fixed now)
  2. https://mywebsite.de/ is the website root with a canonial of https://mywebsite.de
  3. The sitemap plugin lists https://mywebsite.de instead of https://mywebsite.de/ (also an invalid canonial)

This is important for search engines, as they will no correctly crawl the main homepage (which is most important).

Solutions:

  1. Can be fixed via Fix #3127 redirect trailing slash using 301 statuscode #3128

  2. & 3. Can be fixed in multiple places (I guess), you should know best. I've edited Page.php like this (can be improved possibly):

// trim trailing / if not root
if ($url === '') {
    $url = '/';
}
else if ($url !== '/' && rtrim($url, '/') !== rtrim($grav['uri']->base(), '/') ) {
    $url = rtrim($url, '/');
}
@mahagr
Copy link
Member

mahagr commented Jan 15, 2021

Can you try Grav 1.7, it should work there.

@NicoHood
Copy link
Contributor Author

It does not work.

@NicoHood
Copy link
Contributor Author

I've seen the 301 fix which looks good. However the canonical of the root page is still wrong, I guess (unless there was a recent fix I missed).

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

You mean this: <link rel="canonical" href="http://grav.local" />?

@NicoHood
Copy link
Contributor Author

Yes! It should be http://grav.local/

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

Oh, now I think I remember why I didn't do 301 redirects. On the home page, it caused a loop between slash and no slash and never ended up requesting anything from the server.

@NicoHood
Copy link
Contributor Author

Well, that needs to be fixed than. I dont think the redirect code itself makes any difference here, it is just a cosmetic thing for search engines, isnt it?

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

I think it has been fixed, but the main issue was that when you accessed the page, it redirected without ever contacting the server. So the only way to fix that was to clear the cache in the browser.

@NicoHood
Copy link
Contributor Author

Alright, but what about the canonical URL? That should be independent. I've suggested a possible fix in my first post, but there might be better solutions.

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

Well, there was actually a second issue that is not related to SEO.

Create a Grav site and in there create home/index.php (with phpinfo).

Now try to access:

  • /home
  • /home/

The links end up pointing to a different instance, the first one stays in the Grav site, and the second loads the PHP info.

This means that the root page should always have the trailing slash or the home page may not work properly.

@NicoHood
Copy link
Contributor Author

Wow, that is a simple fix. Are you sure this does not break?

For example if you run Grav in a subdir https://example.com/grav and did you also test if language prefixes are used https://example.com/en? The slash should be removed in those cases.

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

I have tested it with language prefixes. Though it will not make any difference as the only slash that will remain is coming from $uri->rootUrl() call, which is always pointing to the path where Grav has been installed.

@mahagr
Copy link
Member

mahagr commented Feb 11, 2021

So basically it redirects https://example.com/grav to https://example.com/grav/ but keeps everything else without the slash. So internally Grav always uses a trailing slash for the root page, even if it is in a subfolder.

I made this change for two reasons:

  • it was a lot simpler fix
  • having url of https://example.com/grav is wrong as it can point to another PHP application in https://example.com and thus not show the Grav home page.

@NicoHood
Copy link
Contributor Author

Sounds reasonable. I will give it a try later this day. Thanks a lot for taking the time!

@NicoHood
Copy link
Contributor Author

Seems to be fixed for me, I will close this. Super nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants