我创建了Laravel CRUD API
,它在localhost(没有.htaccess文件)中可以正常工作,但是在实时服务器上失败。当我发送请求时,它返回404 not found error
,或根据.htaccess
内容下载文件。我应该如何更改.htaccsess
文件以使代码正常工作。这是我的.htaccess
文件,返回未找到错误。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
有一个'官方'的htaccess,应该为您提供基本的代码以使它正常工作。
请参见https://github.com/laravel/laravel/blob/master/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>
然后,只需确保根指向yourproject/public
,使其只能从yourproject/public/index.php
引导。