Laravel:带有斜杠的URL的Apache别名 [英] Laravel: Apache alias for URLs with trailing slash
问题描述
我在目录/home/user/Documents/laravel-training
中有一个Laravel网站.
I have a Laravel website in directory /home/user/Documents/laravel-training
.
我想从 http://localhost/dev/
,
访问我的网站所以我在/etc/apache2/sites-enabled/000-default.conf
中设置了一个别名.
I would like to visit my website from http://localhost/dev/
,
so I set an alias in /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不以斜杠结尾,我可以成功访问我的网站.
Now, I can visit my website successfully in case the URL does not ends with a slash.
例如,如果我添加以下路线:
For example, if I add the following route:
Route::get('test', function() {
return view('welcome');
});
然后我可以从 http://localhost/dev/test
访问它.
但是,如果我的URL是 http://localhost/dev/test/
,则服务器将错误地将我的页面重定向到 http://localhost/test
,并且导致404找不到错误.
But if my URL is http://localhost/dev/test/
, the server will incorrectly redirect my page to http://localhost/test
, and causes a 404 Not Found error.
Not Found
The requested URL /test was not found on this server.
Apache/2.4.12 (Ubuntu) Server at localhost Port 80
有人知道如何解决此问题吗?
Does anyone know how to fix this problem?
回应Stefano Ortisi的评论:
我没有更改 .htaccess
中的其他内容.我只是保留了默认设置.
In response to Stefano Ortisi's comment:
I did not change anything else in .htaccess
. I just kept the default settings.
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>
推荐答案
它不带斜杠重定向就不会出错.
It is not an error that it redirects without the trailing slash.
在您的 public/.htaccess
中,您会看到以下几行:
In your public/.htaccess
as you can see there is the following lines:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
^^^如注释所指出的那样,如果不是文件夹,它将使用结尾的斜杠重定向URL.如果将其删除,您将看到它不会重定向您.
^^^ as the comment states that will redirect a url with a trailing slash if it is not a folder. If you remove that you will see it will not redirect you.
这篇关于Laravel:带有斜杠的URL的Apache别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!