Skip to content

[5.x] Middleware to redirect absolute domains ending in dot#11782

Merged
jasonvarga merged 8 commits intostatamic:5.xfrom
indykoning:feature/redirect-domains-ending-in-dot
May 14, 2025
Merged

[5.x] Middleware to redirect absolute domains ending in dot#11782
jasonvarga merged 8 commits intostatamic:5.xfrom
indykoning:feature/redirect-domains-ending-in-dot

Conversation

@indykoning
Copy link
Copy Markdown
Contributor

@indykoning indykoning commented May 12, 2025

Context: #11714

Domain names ending in dot are completely valid and documented separately in the RFCs
rfc-editor.org/rfc/rfc1035#:~:text=Domain names that end in a dot are called absolute%2C and are taken as complete.

Domain names that end in a dot are called
absolute, and are taken as complete. Domain names which do not end in a
dot are called relative;

rfc-editor.org/rfc/rfc1034#:~:text=character string which represents a complete domain name (often called "absolute"). For example%2C "poneria.ISI.EDU."

a character string which represents a complete domain name (often called "absolute"). For example, "poneria.ISI.EDU."

This causes problems when cache is cleared, the homepage is cached with the . suffix. Every user navigating from the homepage will cause every subsequent cache to contain this . suffix, even when users are not navigating to the site with the . suffix

Where the RFC defines it as being the absolute version of the same domain, browsers dealing with CORS do not agree. Causing issues when CORS is used anywhere on the page.

https://statamic.dev./ has the same problem if you visit the site, and open your console you can see it refuses to load the JS. And all internal links are replaced by https://statamic.dev./
image

This PR introduces an option to redirect domains ending in . to their non . variant e.g. https://example.com./example -> https://example.com/example
preventing urls ending in . from being cached and ensuring the user is sent to a correctly functioning page.

Most major sites seem to deal with it by simply redirecting:

And some do actually break because they don't handle this:

Comment thread src/Providers/AppServiceProvider.php Outdated
@jasonvarga jasonvarga changed the title [5.x] Redirect absolute domains ending in . to regular domain [5.x] Middleware to redirect absolute domains ending in dot May 13, 2025
@jasonvarga
Copy link
Copy Markdown
Member

I've adjusted this so that it doesn't automatically add the middleware. If you want to use it, you can add it yourself in bootstrap/app.php.

 ->withMiddleware(function (Middleware $middleware) {
+    $middleware->append(\Statamic\Http\Middleware\RedirectAbsoluteDomains::class);
 })

It felt too heavy handed to be applied by default. It doesn't feel like the responsibility of the CMS to make such a broad change to your entire application, but I'm happy to make it available to be used.

Also, by only applying it to the web middleware, it wouldn't work when accessing the CP. It should be applied application-wide.

@indykoning
Copy link
Copy Markdown
Contributor Author

Sounds good! Do we want something added to the docs for this so users know to apply this middleware when running into issues?

@duncanmcclean
Copy link
Copy Markdown
Member

Yeah! We can document it after it's been released. Although, if you're eager, feel free to open a PR on the docs repo and we can merge it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants