Laravel项目部署至域名后仅首页可用,其余页面报404错误求助
Hey there, this is a super common snag after deploying Laravel to a domain—let’s walk through the most likely fixes to get your pages working properly:
1. Check Apache mod_rewrite (if using Apache)
Laravel’s clean URLs rely on the mod_rewrite module being enabled. If it’s off, your server won’t redirect requests to Laravel’s front controller (index.php).
- Run this command to enable it:
sudo a2enmod rewrite - Restart Apache to apply changes:
# For Ubuntu/Debian sudo systemctl restart apache2 # For CentOS/RHEL sudo systemctl restart httpd
2. Verify .htaccess Configuration
Make sure your Laravel public directory has a valid .htaccess file (this is included by default in Laravel, but sometimes gets missing or modified during deployment). The correct content should be:
<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>
Also, ensure your Apache virtual host allows .htaccess overrides. Add this to your site’s config:
<Directory /var/www/your-project/public> AllowOverride All Require all granted </Directory>
Restart Apache after making these changes.
3. Fix Nginx Configuration (if using Nginx)
Nginx doesn’t use .htaccess, so you need to configure it to route all requests to index.php. Update your server block to include this:
server { listen 80; server_name your-domain.com; root /var/www/your-project/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; index index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # Adjust to your PHP version fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
Restart Nginx to apply:
sudo systemctl restart nginx
4. Clear Laravel Route Cache
Sometimes old cached routes can cause 404s after deployment. Run these commands from your project root:
php artisan route:clear php artisan route:cache # Optional for production, but clears old cache first
5. Fix File/Directory Permissions
Laravel needs write access to the storage and bootstrap/cache directories. Run these commands to set correct permissions:
sudo chown -R www-data:www-data /var/www/your-project # Use "nginx" instead of "www-data" if on Nginx sudo chmod -R 755 /var/www/your-project/storage sudo chmod -R 755 /var/www/your-project/bootstrap/cache
6. Confirm Virtual Host Points to public Directory
Double-check that your Apache/Nginx virtual host’s root directory is set to Laravel’s public folder—not the project root. Pointing to the root will expose sensitive files and break routing.
Start with the server configuration checks first, since that’s the most common culprit. Let me know if any of these steps resolve your issue!
内容的提问来源于stack exchange,提问作者amiega

