Laravel 5.4部署至000webhost后路由无法访问问题求助
Let's walk through the most likely fixes for this issue—shared hosts like 000webhost often have specific quirks with Laravel's deployment structure that cause routing 404s, even when everything works locally.
1. Fix Your Website Root Directory (Most Common Culprit)
000webhost defaults to using public_html as your website's root directory, but Laravel requires its own public folder to be the root. Here's how to fix this:
- Log into your 000webhost control panel
- Navigate to your domain's settings (usually under "Website" or "Domains")
- Change the root directory from
public_htmltopublic_html/public - Save the changes and test your sign-up/sign-in forms again
This ensures all requests (like /signUp) are routed through Laravel's public/index.php file, which handles your application's routing.
2. Verify Your .htaccess File
Laravel relies on the .htaccess file in the public folder to redirect all requests to index.php. Make sure your public_html/public/.htaccess has the default Laravel content:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
If you copied .htaccess to the public_html root instead, it won't work once you set the root directory to public_html/public.
3. Check Form Action & Route Method Mismatch
Your routes are defined as POST requests, which means accessing /signUp directly via the browser (a GET request) will throw a 404—that's expected behavior. To confirm your form is sending the right request:
- Right-click your sign-up form on the live site and select "View Page Source"
- Check the
actionattribute of the<form>tag—it should matchhttps://myprojectblog.000webhostapp.com/signUp - Ensure the form's
methodis set toPOST(which your code shows it is)
4. Clear Route Cache & Re-Upload Files
Local Laravel projects often cache routes, which can cause mismatches when deployed. Do this locally first:
php artisan route:clear php artisan config:clear
Then re-upload all your Laravel files to the server (especially the bootstrap/cache directory) to ensure no stale cached data is causing issues.
5. Confirm File Permissions
Make sure the following directories have correct read/write permissions (usually 755 or 775 depending on your host's setup):
Blog/storageBlog/bootstrap/cache
Incorrect permissions can sometimes break routing or application loading, even if it's not the direct cause of your 404.
If none of these fixes work, double-check that your public_html/public/index.php paths are correct:
require __DIR__.'/../Blog/bootstrap/autoload.php'; $app = require_once __DIR__.'/../Blog/bootstrap/app.php';
This path looks right (since __DIR__ refers to public_html/public, ../Blog points to your core Laravel files), but it's worth verifying the file structure again to ensure no typos.
内容的提问来源于stack exchange,提问作者Ratnadeep

