Skip to content

Commit

Permalink
Website: Fix deploy-time check for file with PHP-handled redirect (#1350
Browse files Browse the repository at this point in the history
)

## 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
  • Loading branch information
brandonpayton authored Apr 30, 2024
1 parent 37d63b9 commit c04bbc5
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions packages/playground/website-deployment/custom-redirects-lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,25 @@ function playground_handle_request() {
//
$redirect = playground_maybe_redirect( $requested_path );
if ( false !== $redirect ) {
$log( "Redirecting to '${redirect['location']}' with status '${redirect['status']}'" );
header( "Location: ${redirect['location']}" );
http_response_code( $redirect['status'] );
die();
$should_redirect = true;
if ( isset( $redirect['condition']['referers'] ) ) {
$should_redirect = false;
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
foreach ( $redirect['condition']['referers'] as $referer ) {
if ( str_starts_with( $_SERVER['HTTP_REFERER'], $referer ) ) {
$should_redirect = true;
break;
}
}
}
}

if ( $should_redirect ) {
$log( "Redirecting to '${redirect['location']}' with status '${redirect['status']}'" );
header( "Location: ${redirect['location']}" );
http_response_code( $redirect['status'] );
die();
}
}

//
Expand Down Expand Up @@ -159,12 +174,14 @@ function playground_maybe_redirect( $requested_path ) {
);
}

$has_dotorg_referrer = isset( $_SERVER['HTTP_REFERER'] ) && (
str_starts_with( $_SERVER['HTTP_REFERER'], 'https://developer.wordpress.org/' ) ||
str_starts_with( $_SERVER['HTTP_REFERER'], 'https://wordpress.org/' )
);
if ( $has_dotorg_referrer && str_ends_with( $requested_path, '/wordpress.html' ) ) {
if ( str_ends_with( $requested_path, '/wordpress.html' ) ) {
return array(
'condition' => array(
'referers' => array(
'https://developer.wordpress.org/',
'https://wordpress.org/',
),
),
'location' => '/index.html',
'status' => 302,
);
Expand Down

0 comments on commit c04bbc5

Please sign in to comment.