使用PHP内置服务器运行wordpress造成的404问题

Auth:admin Date:2022-06-16 19:11:55 Cat:技术笔记


使用内置服务器时,无法把请求转到发 index.php 。可以通过新加一个 routing.php 文件来实现该功能。

运行 wordpress 时使用如下命令:

php -S localhost:80 routing.php

routing.php 文件的内容如下:

<?php
$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ) )['path'], '/' );

if ( file_exists( $root.$path ) ) {

    // Enforces trailing slash, keeping links tidy in the admin
    if ( is_dir( $root.$path ) && substr( $path, -1 ) !== '/' ) {
        header( "Location: $path/" );
        exit;
    }

    // Runs PHP file if it exists
    if ( strpos( $path, '.php' ) !== false ) {
        chdir( dirname( $root.$path ) );
        require_once $root.$path;
    } else {
        return false;
    }
} else {

    // Otherwise, run `index.php`
    chdir( $root );
    require_once 'index.php';
}

参考:

https://old.romaricpascal.is/writing-about/running-worpdress-with-php-built-in-server/

标签: