配置Apache别名后无法正常访问Laravel站点,请求协助
解决Apache别名访问Laravel的问题
1. 启用mod_rewrite模块
- 打开Apache的
httpd.conf配置文件,找到#LoadModule rewrite_module modules/mod_rewrite.so,删除行首的#注释符号 - 重启Apache服务使配置生效
2. 修正别名与目录配置
在httpd.conf中调整别名对应的目录配置块,确保包含以下核心设置:
Alias /laravel "你的Laravel项目实际路径/public" <Directory "你的Laravel项目实际路径/public"> Options FollowSymLinks MultiViews AllowOverride All Require all granted DirectoryIndex index.php index.html </Directory>
- 将
你的Laravel项目实际路径替换为本地项目的真实路径(如C:/Projects/MyLaravelApp或/var/www/mylaravelapp) AllowOverride All允许Apache读取public目录下的.htaccess文件,这是Laravel路由规则生效的关键DirectoryIndex index.php设置默认入口文件为index.php,避免显示文件夹文件列表
3. 校验public目录的.htaccess文件
确保Laravel项目的public目录下存在.htaccess文件,内容如下:
<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>
若文件丢失,直接复制上述内容创建新的.htaccess文件即可
4. 重启Apache并测试
修改所有配置后,重启Apache服务,然后访问设置的别名(如localhost/laravel),即可正常加载Laravel页面
内容的提问来源于stack exchange,提问作者rnlsn
相关产品推荐
相关产品推荐

