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

Laravel因递归重定向引发内部服务器错误求助

Fixing Internal Server Error (Too Many Internal Redirects) for Laravel 5 on AWS Lightsail Ubuntu 16.04

Looks like you ran into a classic redirect loop issue caused by trying to set DocumentRoot via .htaccess—that’s not the right place for that configuration. Let’s break down how to fix this step by step:

Why This Happens

When you set DocumentRoot in .htaccess, Apache tries to redirect every request to /var/www/html/laravel/public, but the .htaccess itself lives in a parent directory (or the server is conflicting with old configs), creating an infinite redirect loop until it hits the 10-request limit. .htaccess is meant for directory-level rewrite rules, not defining the server’s root directory.

Step-by-Step Solution

1. Update Apache's Virtual Host Configuration

Instead of using .htaccess to set the document root, edit Apache’s site configuration file directly. On Ubuntu 16.04, the default file is at /etc/apache2/sites-available/000-default.conf (if you created a custom site, edit that file instead).

Open the file with your preferred editor:

sudo nano /etc/apache2/sites-available/000-default.conf

Modify the DocumentRoot and <Directory> blocks to point to your Laravel public folder:

DocumentRoot /var/www/html/laravel/public

<Directory /var/www/html/laravel/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Save the file (for nano: Ctrl+O, press Enter, then Ctrl+X).

2. Verify Laravel's Public .htaccess

Make sure the .htaccess file in /var/www/html/laravel/public has the correct default Laravel rewrite rules. If it’s missing or modified, replace it with this:

<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>

3. Enable Mod_Rewrite (If Not Already Enabled)

Laravel relies on Apache’s mod_rewrite module for clean URLs. Enable it with:

sudo a2enmod rewrite

4. Fix Laravel Directory Permissions

Laravel needs write access to the storage and bootstrap/cache directories to function properly. Set the correct permissions:

sudo chown -R www-data:www-data /var/www/html/laravel/storage
sudo chown -R www-data:www-data /var/www/html/laravel/bootstrap/cache
sudo chmod -R 755 /var/www/html/laravel/storage
sudo chmod -R 755 /var/www/html/laravel/bootstrap/cache

5. Restart Apache to Apply Changes

Finally, restart Apache to load all the new configurations:

sudo systemctl restart apache2

After completing these steps, your site should load without the internal server error—the redirect loop will be resolved by correctly setting the document root in Apache’s main config instead of .htaccess.

内容的提问来源于stack exchange,提问作者user6683711

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:32:25