You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel部署到域名后非首页出现404错误的解决咨询

Fixing Laravel 404 Errors on Non-Home Pages After Deployment

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.conf file and make sure this line is uncommented:
    LoadModule rewrite_module modules/mod_rewrite.so
    
    Then restart Apache with sudo 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 public folder has this default .htaccess 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>
    
  • Next, update your Apache virtual host configuration to allow .htaccess overrides. Find the <Directory> block pointing to your Laravel public folder and set AllowOverride All:
    <Directory /var/www/your-project/public>
        AllowOverride All
        Require all granted
    </Directory>
    
    Restart Apache after making this change.

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 the location / 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 -t and restart Nginx with sudo 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:28:36