Skip to content

Instantly share code, notes, and snippets.

@epschmidt
Last active April 16, 2021 19:44
Show Gist options
  • Save epschmidt/babb6ab8d5e44153cefaf4b69570ed61 to your computer and use it in GitHub Desktop.
Save epschmidt/babb6ab8d5e44153cefaf4b69570ed61 to your computer and use it in GitHub Desktop.
gae-app.php and app.yaml setup for WordPress Multisite in subfolders on Google App Engine Standard
# App Engine runtime configuration
runtime: php74
instance_class: F4
# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
# run a long-running PHP script as a worker process (e.g. "php worker.php").
entrypoint: serve gae-app.php
# Defines static handlers to serve WordPress assets
handlers:
# serves static files from the subfolders. i.e. /fr/
- url: /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*\.(htm.*$|html.*$|css.*$|js.*$|ico.*$|jpg.*$|png.*$|gif.*$))
static_files: \2
upload: /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*\.(htm.*$|html.*$|css.*$|js.*$|ico.*$|jpg.*$|png.*$|gif.*$))
secure: always
- url: /(.*\.(htm|html|css|js))
static_files: \1
upload: .*\.(htm|html|css|js)$
secure: always
- url: /wp-content/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: wp-content/\1
upload: wp-content/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
secure: always
- url: /(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: \1
upload: .*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
secure: always
- url: /wp-includes/images/media/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
static_files: wp-includes/images/media/\1
upload: wp-includes/images/media/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
secure: always
- url: /.*
secure: always
script: auto
<?php
/**
* This file handles all routing for a WordPress project running on App Engine.
* It serves up the appropriate PHP file depending on the request URI.
*
* @see https://cloud.google.com/appengine/docs/standard/php7/runtime#application_startup
*/
/**
* Function to return a PHP file to load based on the request URI.
*
* @param string $full_request_uri The request URI derivded from $_SERVER['REQUEST_URI'].
*/
function get_real_file_to_load($full_request_uri)
{
$request_uri = @parse_url($full_request_uri)['path'];
// Redirect /wp-admin to /wp-admin/ (adds a trailing slash)
if ($request_uri === '/wp-admin') {
header('Location: /wp-admin/');
exit;
}
// Redirect /subfolder/wp-admin to /subfolder/wp-admin/ (adds a trailing slash)
if ($request_uri === '/fr/wp-admin') {
header('Location: /fr/wp-admin/');
exit;
}
// Serve up "index.php" when /wp-admin/ is requested
if ($request_uri === '/wp-admin/') {
return '/wp-admin/index.php';
}
// Fix for the network page redirecting to main page
if ($request_uri === '/wp-admin/network/') {
return '/wp-admin/network.php';
}
// Serve up "index.php" when /subfolder/wp-admin/ is requested
if ($request_uri === '/fr/wp-admin/') {
return '/wp-admin/index.php';
}
// Fix for login redirect heading back to the subfolder page
if ($request_uri === '/fr/wp-login') {
return '/wp-login.php';
}
// Fix for subfolder content paths
if (strpos($request_uri, '/fr/wp-') !== false) {
return str_replace("/fr/wp-", "/wp-", $request_uri);
}
// Load the file requested if it exists
if (is_file(__DIR__ . $request_uri)) {
return $request_uri;
}
// Send everything else through index.php
return '/index.php';
}
// fixes b/111391534
$_SERVER['HTTPS'] = $_SERVER['HTTP_X_APPENGINE_HTTPS'];
// Loads the expected WordPress framework file
// (e.g index.php, wp-admin/* or wp-login.php)
$file = get_real_file_to_load($_SERVER['REQUEST_URI']);
// Set the environment variables to reflect the script we're loading
// (in order to trick WordPress)
$_SERVER['DOCUMENT_URI'] = $_ENV['DOCUMENT_URI'] = $file;
$_SERVER['PHP_SELF'] = $_ENV['PHP_SELF'] = $file;
$_SERVER['SCRIPT_NAME'] = $_ENV['SCRIPT_NAME'] = $file;
$_SERVER['SCRIPT_FILENAME'] = $_ENV['SCRIPT_FILENAME'] = __DIR__ . $file;
require __DIR__ . $file;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment