PHP MVC部署故障求助:线上服务器路由控制器返回404错误
Hey there, let's work through your MVC routing and form submission headaches on your live server. I’ve gone through your code snippets, and here are the most likely fixes to resolve those 404s and missing $_POST data:
1. Fix the .htaccess RewriteBase (Most Common Culprit)
Localhost runs your app at the root, but most live servers host apps in a subdirectory (e.g., https://yourdomain.com/your-app/). Your current RewriteBase / tells Apache to look for files at the server root, not your app’s actual directory.
Update RewriteBase:
If your app lives in a subdirectory likemy-mvc-app, change this line in.htaccess:RewriteBase /my-mvc-app/If it’s at the server root, keep
RewriteBase /but verify your server allows mod_rewrite (ask your host or checkphpinfo()formod_rewrite).Adjust the RewriteRule:
Some servers don’t handle the?in your rule well. Swap it for a slash:RewriteRule ^(.*)$ index.php/$1 [QSA,L]
2. Fix Route.php to Handle Server-Specific URI Paths
On live servers, $_SERVER['REQUEST_URI'] includes your app’s subdirectory (e.g., /my-mvc-app/admin/login), but your code currently treats the first segment (my-mvc-app) as the controller name—hence the 404. Clean up the URI before processing:
Update your Route.php constructor to strip out the base path:
public function __construct($config) { // Get the base path (remove index.php from the script path) $basePath = str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); // Remove the base path from the request URI to get clean route segments $cleanUri = str_replace($basePath, '', $_SERVER['REQUEST_URI']); // Now split the cleaned URI into segments $url = explode('/', trim($cleanUri, '/')); // ... rest of your existing code }
3. Fix the Login Form Action Path
Your form uses an absolute path /admin/login, which points to https://yourdomain.com/admin/login instead of https://yourdomain.com/your-app/admin/login on live servers. This causes the POST request to hit a non-existent endpoint, so $_POST is empty.
- Use a base URL from your config:
First, add abase_urlentry to yourConfig.phpreturn array:
Then update the form action in'base_url' => 'https://yourdomain.com/your-mvc-app/',login.php:
Alternatively, use a relative path if the form is loaded from the admin section:<form action="<?php echo $config['base_url']; ?>admin/login" method="post"><form action="admin/login" method="post">
4. Verify File Permissions
Live servers are strict about permissions. Ensure:
- Controller files in
app/Controllers/have 644 permissions (readable by the server) - Directories like
app/andsystem/have 755 permissions (accessible by the server)
If permissions are wrong, PHP can’t find your controller classes, triggering the 404.
5. Debug Route Variables (For Quick Troubleshooting)
Add debug output to Route.php to see exactly what the server is processing:
public function __construct($config) { var_dump($_SERVER['REQUEST_URI']); $url = explode('/', trim($_SERVER['REQUEST_URI'], '/')); var_dump($url); var_dump($config['namespace'].$controller); exit; }
This will show you if the server is picking up the wrong controller/method names (e.g., your subdirectory instead of admin).
Start with the .htaccess fixes—they’re the most common cause of this exact issue. If that doesn’t work, move to the URI cleanup in Route.php.
内容的提问来源于stack exchange,提问作者wuDead

