Laravel因递归重定向引发内部服务器错误求助
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

