月度归档: 2021 年 1 月

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

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

    运行 wordpress 时使用如下命令:

    E:\php\php-7.4.29-Win32-vc15-x64\php.exe -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