Hitting 404 loading index.php locally
-
Trying to set up a WordPress Multi site locally with Lando. But I am hitting a 404. Though it does not seem to be a Lando issue as I can run other basic WordPress websites with it I added a comment to a similar issue https://github.com/lando/lando/issues/1264 .
I did set up matters with Nginx, not Apache, but did import database, set up system WP files, added production plugins, themes. Also added Lando config to load local database data to
wp-config.php
/** This will ensure these are only loaded on Lando */
if (getenv('LANDO')) {
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpress');
/** MySQL database password */
define('DB_PASSWORD', 'wordpress');
/** MySQL hostname */
define('DB_HOST', 'database');
/** URL routing (Optional, may not be necessary) */
define('WP_HOME','https://autentical.lndo.site/');
define('WP_SITEURL','https://autentical.lndo.site/');
}
define( 'SUNRISE', 'on' );
/**define( 'COOKIE_DOMAIN', '' );
define( 'ADMIN_COOKIE_PATH', '/' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );
...The file
sunrise.php
for custom routing including for WPML has<?php
if ( !defined( 'SUNRISE_LOADED' ) )
define( 'SUNRISE_LOADED', 1 );
if ( defined( 'COOKIE_DOMAIN' ) ) {
die( 'The constant "COOKIE_DOMAIN" is defined (probably in wp-config.php). Please remove or comment out that define() line.' );
}
$wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
$dm_domain = $_SERVER[ 'HTTP_HOST' ];
if( ( $nowww = preg_replace( '|^www\.|', '', $dm_domain ) ) != $dm_domain )
$where = $wpdb->prepare( 'domain IN (%s,%s)', $dm_domain, $nowww );
else
$where = $wpdb->prepare( 'domain = %s', $dm_domain );
$wpdb->suppress_errors();
$domain_mapping_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->dmtable} WHERE {$where} ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
$wpdb->suppress_errors( false );
if( $domain_mapping_id ) {
$current_blog = $wpdb->get_row("SELECT * FROM {$wpdb->blogs} WHERE blog_id = '$domain_mapping_id' LIMIT 1");
$current_blog->domain = $dm_domain;
$current_blog->path = '/';
$blog_id = $domain_mapping_id;
$site_id = $current_blog->site_id;
define( 'COOKIE_DOMAIN', $dm_domain );
$current_site = $wpdb->get_row( "SELECT * from {$wpdb->site} WHERE id = '{$current_blog->site_id}' LIMIT 0,1" );
$current_site->blog_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain='{$current_site->domain}' AND path='{$current_site->path}'" );
if ( function_exists( 'get_site_option' ) )
$current_site->site_name = get_site_option( 'site_name' );
elseif ( function_exists( 'get_current_site_name' ) )
$current_site = get_current_site_name( $current_site );
define( 'DOMAIN_MAPPING', 1 );
}
/**
* WPML Sunrise Script - START
* @author OnTheGoSystems
* @version 3.7.0
*
* Place this script in the wp-content folder and add "define('SUNRISE', 'on');" in wp-config.php
* in order to enable using different domains for different languages in multisite mode
*
* Experimental feature
*/
/**
* Class WPML_Sunrise_Lang_In_Domains
* @author OnTheGoSystems
*/
class WPML_Sunrise_Lang_In_Domains {
/** @var wpdb $wpdb */
private $wpdb;
/** @var string $table_prefix */
private $table_prefix;
/** @var string $current_blog */
private $current_blog;
/** @var bool $no_recursion */
private $no_recursion;
/**
* Method init
*/
public function init() {
if ( ! defined( 'WPML_SUNRISE_MULTISITE_DOMAINS' ) ) {
define( 'WPML_SUNRISE_MULTISITE_DOMAINS', true );
}
add_filter( 'query', array( $this, 'query_filter' ) );
}
/**
* @param string $q
*
* @return string
*/
public function query_filter( $q ) {
$this->set_private_properties();
if ( ! $this->current_blog && ! $this->no_recursion ) {
$this->no_recursion = true;
$domains = $this->extract_variables_from_query( $q, 'domain' );
if ( $domains && $this->query_has_no_result( $q ) ) {
$q = $this->transpose_query_if_one_domain_is_matching( $q, $domains );
}
$this->no_recursion = false;
}
return $q;
}
/**
* method set_private_properties
*/
private function set_private_properties() {
global $wpdb, $table_prefix, $current_blog;
$this->wpdb = $wpdb;
$this->table_prefix = $table_prefix;
$this->current_blog = $current_blog;
}
/**
* @param string $query
*
* @return array
*/
private function extract_variables_from_query( $query, $field ) {
$variables = array();
$patterns = array(
'#WHERE\s+' . $field . '\s+IN\s*\(([^\)]+)\)#',
'#WHERE\s+' . $field . '\s*=\s*([^\s]+)#',
'#AND\s+' . $field . '\s+IN\s*\(([^\)]+)\)#',
'#AND\s+' . $field . '\s*=\s*([^\s]+)#',
);
foreach ( $patterns as $pattern ) {
$found = preg_match( $pattern, $query, $matches );
if ( $found && array_key_exists( 1, $matches ) ) {
$variables = $matches[1];
$variables = preg_replace( '/\s+/', '', $variables );
$variables = preg_replace( '/[\'"]/', '', $variables );
$variables = explode( ',', $variables );
break;
}
}
return $variables;
}
/**
* @param string $q
*
* @return bool
*/
private function query_has_no_result( $q ) {
return ! (bool) $this->wpdb->get_row( $q );
}
/**
* @param string $q
* @param array $domains
*
* @return string
*/
private function transpose_query_if_one_domain_is_matching( $q, $domains ) {
$paths = $this->extract_variables_from_query( $q, 'path' );
// Create as many placeholders as $paths we have.
$placeholders = implode( ',', array_fill( 0, sizeof( $paths ), '%s' ) );
// Array with all the parameters for preparing the SQL.
$parameters = $paths;
$parameters[] = BLOG_ID_CURRENT_SITE;
// The ORDER is there to get the default site at the end of the results.
$blogs = $this->wpdb->get_col( $this->wpdb->prepare(
"SELECT blog_id FROM {$this->wpdb->blogs} WHERE path IN ($placeholders) ORDER BY blog_id = %d",
$parameters
) );
$found_blog_id = null;
foreach ( (array) $blogs as $blog_id ) {
$prefix = $this->table_prefix;
if ( $blog_id > 1 ) {
$prefix .= $blog_id . '_';
}
$icl_settings = $this->wpdb->get_var( "SELECT option_value FROM {$prefix}options WHERE option_name = 'icl_sitepress_settings'" );
if ( $icl_settings ) {
$icl_settings = unserialize( $icl_settings );
if ( $icl_settings && 2 === (int) $icl_settings['language_negotiation_type'] ) {
$found_blog_id = $this->get_blog_id_from_domain( $domains, $icl_settings, $blog_id );
if ( $found_blog_id ) {
$q = $this->wpdb->prepare( "SELECT blog_id FROM {$this->wpdb->blogs} WHERE blog_id = %d", $found_blog_id );
break;
}
}
}
}
return $q;
}
/**
* @param array $domains
* @param array $wpml_settings
* @param $blog_id
*
* @return mixed
*/
private function get_blog_id_from_domain( array $domains, array $wpml_settings, $blog_id ) {
foreach ( $domains as $domain ) {
if ( in_array( 'https://' . $domain, $wpml_settings['language_domains'], true ) ) {
return $blog_id;
} elseif ( in_array( $domain, $wpml_settings['language_domains'], true ) ) {
return $blog_id;
}
}
return null;
}
}
$wpml_sunrise_lang_in_domains = new WPML_Sunrise_Lang_In_Domains();
$wpml_sunrise_lang_in_domains->init();
/**
* WPML Sunrise Script - END
*/Lando
.lando.yml
hasname: autentical
recipe: wordpress
config:
webroot: .
php: "8.3"
via: nginx
database: mysql:8.0Why Nginx hits a 404 reaching index.php
appserver_nginx_1 | nginx 03:51:16.38 INFO ==> ** NGINX setup finished! **
appserver_nginx_1 |
appserver_nginx_1 | nginx 03:51:16.39 INFO ==> ** Starting NGINX **
appserver_nginx_1 | 172.19.0.2 - - [14/Jan/2025:03:51:35 +0000] "GET / HTTP/1.1" 404 47 "-" "axios/1.7.7"
appserver_nginx_1 | 172.20.0.1 - - [14/Jan/2025:03:51:35 +0000] "GET / HTTP/1.1" 404 47 "-" "axios/1.7.7"
appserver_nginx_1 | 172.19.0.2 - - [14/Jan/2025:03:51:36 +0000] "GET / HTTP/1.1" 404 47 "-" "axios/1.7.7"
appserver_nginx_1 | 172.20.0.1 - - [14/Jan/2025:03:51:36 +0000] "GET / HTTP/1.1" 404 47 "-" "axios/1.7.7"
appserver_nginx_1 | 172.19.0.2 - - [14/Jan/2025:03:53:22 +0000] "GET / HTTP/1.1" 404 47 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0"is not clear to me. Is that because WP Multi Site /
sunrise.php
is looking for another index.php than the one in the root? Or is it because the Lando basic Nginx setup does not work? Or is it because the WP Multi Site setup is normally different from the one I have locally?I am looking into https://gist.github.com/kyletaylored/79e40b0f2f39b471945286769961dfa5 and comparing notes to default setup with Lando for WordPress. If anyone here however has experience and some solid tips i am all ears.
- You must be logged in to reply to this topic.