迁移服务器后WordPress报REQUEST_URI未定义,无法访问wp-admin
Hey there, let's tackle this REQUEST_URI error you're facing. Since you've already ruled out themes and plugins by swapping them out and disabling all plugins, the issue is almost certainly tied to your new server's configuration—here are the targeted fixes you can try:
1. Check Your Server Type (Apache vs. Nginx)
This error is super common when migrating from Apache to Nginx, because Nginx doesn't pass the REQUEST_URI environment variable to PHP by default.
If you're using Nginx:
Add the fastcgi_param REQUEST_URI $request_uri; line to your site's Nginx configuration file, inside the location ~ \.php$ block. Here's what the updated block should look like:
location ~ \.php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Adjust this path to match your PHP-FPM socket fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; # This line is critical include fastcgi_params; }
After adding this, restart Nginx with sudo systemctl restart nginx (or your server's equivalent restart command).
2. Adjust PHP Configuration
If you're using Apache and still seeing the error, the problem might be with how PHP is handling environment variables:
- Open your
php.inifile (location varies by server—common paths are/etc/php/7.4/apache2/php.inior/usr/local/php/php.ini). - Look for the
variables_orderdirective. If it's set tovariables_order = "GPCS", change it tovariables_order = "EGPCS"(theEstands for environment variables). - Restart your Apache server or PHP-FPM service to apply the change.
Temporary Emergency Fix:
If you need a quick workaround while sorting out server configs, add this code to the top of your WordPress wp-config.php file (before any other WordPress-specific code):
// Manually define REQUEST_URI if it's missing if (!isset($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''); }
Note: This is a band-aid—always fix the root server config issue long-term.
3. Verify .htaccess and Apache Modules
For Apache servers:
- Make sure your WordPress
.htaccessfile has the default rewrite rules (if you haven't customized it):
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
- Confirm that the
mod_rewritemodule is enabled. Runsudo a2enmod rewriteto enable it, then restart Apache withsudo systemctl restart apache2.
One of these steps should resolve the undefined REQUEST_URI error—start with checking your server type since that's the most frequent culprit when moving to a new server. Let me know if you need help with any specific part of these fixes!
内容的提问来源于stack exchange,提问作者user3501194

