Serve index fallback on my.wordpress.net - #3611
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds an SPA-style index.html fallback specifically for my.wordpress.net requests so deep links don’t 404 when no static file / redirect rule matches.
Changes:
- Add
my.wordpress.nethost detection (including stripping port) to gate SPA fallback behavior. - When path resolution fails for
my.wordpress.net, rewrite to/index.htmland resolve its deployed location (includingstatic-files-to-serve-via-php/). - Introduce helper functions to centralize host matching and index fallback resolution.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function playground_is_my_wordpress_net_request() { | ||
| $host = strtolower( $_SERVER['HTTP_HOST'] ?? '' ); | ||
| $host = preg_replace( '/:\d+$/', '', $host ); | ||
|
|
||
| return 'my.wordpress.net' === $host; | ||
| } |
There was a problem hiding this comment.
@brandonpayton do you know if this would also be my.wordpress.net?
There was a problem hiding this comment.
@akirk I wouldn't know without testing on another WP Cloud site. For this, you could ssh into a WoA site, add a test.php page, and try things out.
But HTTP_HOST should be fine since the hostname is what the WP Cloud request routing uses to identify the site to begin with. If the request makes it to the my.wordpress.net site, it is because the request's Host header matches a domain associated with that site, and my.wordpress.net should be the only domain associated with that particular WP Cloud site.
There was a problem hiding this comment.
Yeah, so do you think we could equally well just merge this PR and deploy my.wordpress.net?
| function playground_resolve_my_wordpress_net_index_fallback() { | ||
| $resolved_path = realpath( __DIR__ . '/index.html' ); | ||
| if ( false === $resolved_path ) { | ||
| // Deployment may move index.html aside so PHP can apply custom cache headers. | ||
| $resolved_path = realpath( __DIR__ . '/static-files-to-serve-via-php/index.html' ); | ||
| } | ||
|
|
||
| return $resolved_path; | ||
| } |
| if ( false === $resolved_path && playground_is_my_wordpress_net_request() ) { | ||
| $requested_path = '/index.html'; | ||
| $resolved_path = playground_resolve_my_wordpress_net_index_fallback(); | ||
| } |
|
I've tried to test-deploy it but the branch protections prevent it, so I guess we'll need to merge it to trunk. |
| } | ||
|
|
||
| if ( false === $resolved_path && ! str_ends_with( $requested_path, '.php' ) ) { | ||
| if ( false === $resolved_path && ! str_ends_with( $served_path, '.php' ) ) { |
There was a problem hiding this comment.
This looks like a good clarification.
brandonpayton
left a comment
There was a problem hiding this comment.
I have not tested this, but it reads well and makes sense to me.
| $host = strtolower( $_SERVER['HTTP_HOST'] ?? '' ); | ||
| $host = preg_replace( '/:\d+$/', '', $host ); | ||
|
|
||
| return 'my.wordpress.net' === $host; |
There was a problem hiding this comment.
ignorable nit: If we're already using a regex for replacement, maybe this could be a one-liner regex match on /^my.wordpress.net(:\d+)?$/.
| return false; | ||
| } | ||
|
|
||
| function playground_is_my_wordpress_net_request() { |
There was a problem hiding this comment.
Since personal-wp is not the same as the Playground webapp, we may eventually want to fork these deployment-related scripts so customizing my.wordpress.net redirects doesn't introduce any risk to playground.wordpress.net and vice versa.
|
I'm not sure if WP Cloud nginx config may interfere in any way with a request for a WP path like |
Co-authored-by: Brandon Payton <brandon@happycode.net>
|
Just cycling back that this works as expected after the deploy. |
What?
On my prototype, I redirected 404s to index.html so that my.wordpress.net can be served also for those URLs and then simply restore the URL. Before this PR those URLs lead to a white page instead of loading my.wordpress.net.
Why?
By serving index.html for 404s, it allows to make my.wordpress.net behave like a real WordPress.
How?
The shared WP Cloud redirect handler now falls back to index.html only when HTTP_HOST is my.wordpress.net, after normal file resolution and static-files-to-serve-via-php lookup have both failed. The fallback also checks the deployed static-files-to-serve-via-php/index.html location because deployment may move index.html there for custom cache headers.
Testing