【发布时间】:2016-02-19 16:01:24
【问题描述】:
我在/home/user/Documents/laravel-training 目录中有一个 Laravel 网站。
我想通过http://localhost/dev/访问我的网站,
所以我在/etc/apache2/sites-enabled/000-default.conf中设置了一个别名。
这是我的000-default.conf:
<VirtualHost *:80>
ServerName foo.example.net
ServerAdmin foo@example.org
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /dev /home/user/Documents/laravel-training/public
<Directory /home/user/Documents/laravel-training/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
我还在/home/user/Documents/laravel-training/public/.htaccess的开头添加了RewriteBase /dev。
现在,如果 URL 不以斜杠结尾,我可以成功访问我的网站。
例如,如果我添加以下路线:
Route::get('test', function() {
return view('welcome');
});
然后我可以通过http://localhost/dev/test访问它。
但是如果我的 URL 是 http://localhost/dev/test/,服务器会错误地将我的页面重定向到 http://localhost/test,并导致 404 Not Found 错误。
Not Found
The requested URL /test was not found on this server.
Apache/2.4.12 (Ubuntu) Server at localhost Port 80
有人知道如何解决这个问题吗?
回应 Stefano Ortisi 的评论:
我没有更改.htaccess 中的任何其他内容。我只是保留了默认设置。
RewriteBase /dev
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
【问题讨论】:
-
您是否仔细检查了 laravel 公用文件夹中的 .htaccess 是否正常工作?
-
@StefanoOrtisi 我没有修改任何其他内容。我已经更新了我的帖子。
-
尝试将
RewriteBase /dev指令移到RewriteEngine On指令之后。 -
@patricus 我试过了,问题还是出现了。