Skip to content

Commit c04bbc5

Browse files
Website: Fix deploy-time check for file with PHP-handled redirect (#1350)
## What is this PR doing? This PR solves an issue where `/wordpress.html` was not being served by PHP because it was not moved into `files-to-serve-via-php` during website deploy. And it was not being moved because the should-serve-via-PHP check relied upon the current referer, which is something we don't know at deploy time. ## How is the problem addressed? This PR solves the issue by having the maybe-redirect function return a declaration of its intent to redirect for specific referers. Then we can see there is need for special treatment at deploy time, and the request handler can see the declaration and act upon it at request time. ## Testing Instructions - Tested manually via SSH on playground-dot-wordpress-dot-net.atomicsites.blog. - Will also test post-deploy by manually running the deploy workflow for WP Cloud and confirming the fix afterward Related to #1197
1 parent 37d63b9 commit c04bbc5

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

Diff for: packages/playground/website-deployment/custom-redirects-lib.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,25 @@ function playground_handle_request() {
4545
//
4646
$redirect = playground_maybe_redirect( $requested_path );
4747
if ( false !== $redirect ) {
48-
$log( "Redirecting to '${redirect['location']}' with status '${redirect['status']}'" );
49-
header( "Location: ${redirect['location']}" );
50-
http_response_code( $redirect['status'] );
51-
die();
48+
$should_redirect = true;
49+
if ( isset( $redirect['condition']['referers'] ) ) {
50+
$should_redirect = false;
51+
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
52+
foreach ( $redirect['condition']['referers'] as $referer ) {
53+
if ( str_starts_with( $_SERVER['HTTP_REFERER'], $referer ) ) {
54+
$should_redirect = true;
55+
break;
56+
}
57+
}
58+
}
59+
}
60+
61+
if ( $should_redirect ) {
62+
$log( "Redirecting to '${redirect['location']}' with status '${redirect['status']}'" );
63+
header( "Location: ${redirect['location']}" );
64+
http_response_code( $redirect['status'] );
65+
die();
66+
}
5267
}
5368

5469
//
@@ -159,12 +174,14 @@ function playground_maybe_redirect( $requested_path ) {
159174
);
160175
}
161176

162-
$has_dotorg_referrer = isset( $_SERVER['HTTP_REFERER'] ) && (
163-
str_starts_with( $_SERVER['HTTP_REFERER'], 'https://developer.wordpress.org/' ) ||
164-
str_starts_with( $_SERVER['HTTP_REFERER'], 'https://wordpress.org/' )
165-
);
166-
if ( $has_dotorg_referrer && str_ends_with( $requested_path, '/wordpress.html' ) ) {
177+
if ( str_ends_with( $requested_path, '/wordpress.html' ) ) {
167178
return array(
179+
'condition' => array(
180+
'referers' => array(
181+
'https://developer.wordpress.org/',
182+
'https://wordpress.org/',
183+
),
184+
),
168185
'location' => '/index.html',
169186
'status' => 302,
170187
);

0 commit comments

Comments
 (0)