Laravel部署到域名后非首页出现404错误的解决咨询
Hey there! Let's sort out that frustrating 404 issue you're hitting with your Laravel project. Since your local setup works perfectly but only the homepage loads on your domain, this is almost always tied to URL rewriting misconfiguration on your web server. Here's how to fix it step by step:
1. Check Apache mod_rewrite (if using Apache)
If your server runs Apache, the mod_rewrite module is required to route requests to Laravel's index.php file.
- On Ubuntu/Debian systems, enable it with this command:
sudo a2enmod rewrite sudo systemctl restart apache2 - On CentOS/RHEL, open your
httpd.conffile and make sure this line is uncommented:
Then restart Apache withLoadModule rewrite_module modules/mod_rewrite.sosudo systemctl restart httpd.
2. Verify the .htaccess File & Apache Directory Permissions
Laravel's public directory needs a valid .htaccess file to handle rewrites.
- First, make sure your project's
publicfolder has this default.htaccesscontent:<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> - Next, update your Apache virtual host configuration to allow
.htaccessoverrides. Find the<Directory>block pointing to your Laravelpublicfolder and setAllowOverride All:
Restart Apache after making this change.<Directory /var/www/your-project/public> AllowOverride All Require all granted </Directory>
3. Correct Nginx Configuration (if using Nginx)
Nginx doesn't use .htaccess, so you need to add rewrite rules directly to your server block.
- Open your Nginx server config file (usually in
/etc/nginx/sites-available/) and update thelocation /block to include:server { listen 80; server_name your-domain.com; root /var/www/your-project/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } # PHP processing location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Adjust PHP version as needed } } - Test the config with
sudo nginx -tand restart Nginx withsudo systemctl restart nginx.
4. Clear Laravel Cache
Sometimes stale cache can cause unexpected routing issues. Run these commands in your project root:
php artisan route:clear php artisan config:clear php artisan cache:clear
5. Confirm Server Root Points to the public Folder
Double-check that your web server's document root is set to Laravel's public directory, not the project root. This is a common mistake—pointing to the root folder not only causes routing issues but also exposes sensitive files like .env.
Once you've gone through these steps, your non-homepage routes should load correctly. If you're still stuck, double-check that your server has the correct permissions for the Laravel project files!
内容的提问来源于stack exchange,提问作者MJ DEV

