Laravel项目通过虚拟主机访问时显示public目录文件列表问题
解决Laravel虚拟主机显示文件列表问题
问题描述
配置虚拟主机运行Laravel项目时,访问域名显示public目录的文件列表,而非正常启动项目;新建Laravel项目后问题依旧。当前访问效果如下:
配置信息
虚拟主机配置
<VirtualHost *:80> ServerAdmin wosomtest.com ServerName wosomtest.com ServerAlias wosomtest.com DocumentRoot "/usr/local/var/www/wosom/public" <Directory "/usr/local/var/www/wosom/public"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted </Directory> ErrorLog "/Applications/AMPPS/apps/apache/logs/error.log" CustomLog "/Applications/AMPPS/apps/apache/logs/access.log" combined </VirtualHost>
public目录.htaccess配置
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ index.php [L] </IfModule>
解决步骤
关闭目录索引功能
虚拟主机配置的<Directory>块中,Options Indexes ...里的Indexes会让Apache在找不到默认首页时显示文件列表。将其修改为:Options -Indexes FollowSymLinks Includes ExecCGI设置默认首页
在<Directory>块中添加DirectoryIndex指令,指定index.php为默认入口:DirectoryIndex index.php完整的
<Directory>块示例:<Directory "/usr/local/var/www/wosom/public"> Options -Indexes FollowSymLinks Includes ExecCGI DirectoryIndex index.php AllowOverride All Require all granted </Directory>确认mod_rewrite模块已启用
- 打开Apache主配置文件(如
httpd.conf),检查是否存在LoadModule rewrite_module modules/mod_rewrite.so,确保该行没有被注释(移除行首的#)。 - 修改后重启Apache服务。
- 打开Apache主配置文件(如
修复.htaccess规则
替换现有.htaccess内容为Laravel标准规则:<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>重启Apache服务
执行命令重启Apache使配置生效:sudo apachectl restart
内容的提问来源于stack exchange,提问作者Jack Hickson
相关产品推荐
相关产品推荐

