[英]Unable to get .htaccess to do 301 redirects correctly in a Laravel app
[英]Laravel and Apache2 : unable to correctly use redirects
我对Laravel有问题。 在运行Debian 8.5的名为fmbsrv130的Apache 2.4.10 Web服务器上,我想安装一个名为sentinelle的Laravel项目。
这样就可以安装Laravel了,我可以使用http:// fmbsrv130 / sentinelle /来访问它(我有Laravel的欢迎页面)。 但是,如果我尝试转到http:// fmbsrv130 / sentinelle / 1 (不存在),我从Apache2收到404,内容为“在以下位置找不到所请求的URL /home/webadmin/sentinelle/public/index.php。该服务器。” 如果我尝试访问http://fmbsrv130/sentinelle/index.php/1,我从Laravel获得404:“对不起,找不到您要查找的页面。RouteCollection.php第161行中有1/1 NotFoundHttpException: ...”
rewrite_mod已在Apache中正确启用。
这是Apache的配置文件(在sites-available中为000-default.conf):
Alias "/sentinelle" "/home/webadmin/sentinelle/public/"
<Directory "/home/webadmin/sentinelle/public/">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
为了进行测试,如果我尝试使用以下命令从Laravel的公共文件夹中修改.htaccess文件
RewriteEngine On
RewriteRule ^.*$ htaccess_tester.php
(htaccess_tester.php是用于测试.htaccessfile内容的文件(在GitHub上查看),当前位于公共文件夹中),我有Apache2 404错误消息The requested URL /home/webadmin/sentinelle/public/htaccess_tester.php was not found on this server.
是.htaccess文件的问题,还是Apache2配置的问题,还是Laravel配置的问题,还是文件访问权限?
是在apache2上启用了mod_rewrite功能,请确认应将其启用.htaccess工作,并在.htaccess文件中添加默认的laravel .htaccess内容
<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>
另外,如果这不起作用,请尝试从公共目录中删除index.php并将其放置在/sentinell
根目录中,然后将.htaccess修改为RewriteRule ^ public/index.php [L]
好的,最后我通过进行一些修改找到了一种解决方案:第一个是将.htaccess文件放入公用文件夹:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /sentinelle/
# 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>
(添加RewriteBase / sentinelle /)
第二个是进入default.conf Apache文件:
Alias /sentinelle /home/webadmin/sentinelle/public/
<Directory "/home/webadmin/sentinelle/public">
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
它解决了本主题中描述的问题,我将打开另一个,因为现在我的地址中有最后一个“ /”的问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.