Laravel访问项目根目录显示文件列表而非跳转至public/index.php
Hey there, let's get this sorted out for you. Since you've already checked AllowOverride All and mod_rewrite, here are a few targeted solutions to make https://example.com/play/play1 load your Laravel homepage directly:
1. Update Apache Configuration to Point to the public Directory
The cleanest approach is to tell Apache to serve content from Laravel's public folder when accessing /play/play1.
- Open your Apache main config file (usually
/etc/httpd/conf/httpd.conf) or a virtual host file in/etc/httpd/conf.d/. - Add these lines at the end (adjust paths if needed):
# Map the /play/play1 URL to the Laravel public directory Alias "/play/play1" "/var/www/html/play/play1/public" # Ensure proper access and rewrite rules for the public folder <Directory "/var/www/html/play/play1/public"> AllowOverride All Require all granted Options -Indexes # Optional: Disable directory listing entirely </Directory> - Save the file and restart Apache:
sudo systemctl restart httpd
2. Add a Root-Level .htaccess to Redirect to public
If you can't modify Apache's main config (or prefer a project-specific fix), create a .htaccess file directly in your Laravel project root (/var/www/html/play/play1) with these rules:
RewriteEngine On # Redirect all requests to the public subdirectory RewriteRule ^(.*)$ public/$1 [L]
Then set the correct permissions so Apache can read it:
sudo chown apache:apache /var/www/html/play/play1/.htaccess sudo chmod 644 /var/www/html/play/play1/.htaccess
3. Double-Check Directory Listing is Disabled
If you still see the file list after trying the above, make sure directory indexing is turned off for your project root. Add this block to your Apache config:
<Directory "/var/www/html/play/play1"> Options -Indexes AllowOverride All Require all granted </Directory>
After making any config changes, always restart Apache to apply them. Also, test in an incognito window to avoid browser cache issues.
内容的提问来源于stack exchange,提问作者Catalin T

