[英]How do I get URL rewrite to work in Laravel 5?
除了基本 URL 和/site/public/
文件夾中的文件之外,我無法使任何 Laravel 路由正常工作。
http://testurl/
(通過 apache vhost 設置)工作,並加載主頁。
http://testurl/blog
) 不起作用(返回 404),但我希望它起作用。
奇怪的是, http://testurl/index.php/blog
確實有效。
我在一百個不同的地方用谷歌搜索過,看了無數的 StackOverflow 問題,並嘗試了我能找到的所有東西,但沒有任何效果。
我怎樣才能解決這個問題?
我在 Apache 2.4.29 上運行 Laravel 5.7,在 MacOSX 10.13.4 上運行 PHP 7.1.9。
我在httpd.conf
啟用了重寫模塊,如下所示:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
...
<Directory />
AllowOverride all
Require all denied
</Directory>
我的httpd-vhosts.conf
設置如下:
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents/site/public"
ServerName testurl
<Directory /Library/WebServer/Documents/site/public>
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
/site/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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
我的config/app.php
有以下幾行:
'url' => env('APP_URL', 'http://testurl'),
最后但並非最不重要的是, web.php
看起來像這樣:
<?php
Route::get('/', function () {
return view('home');
});
Route::get('blog', function () {
return view('blog');
});
如果還有什么我遺漏的(或我可以嘗試的任何東西),請告訴我。
最好,我想要在部署到服務器時不需要我更改 Laravel 代碼的解決方案。
謝謝您的幫助!
經過大量搜索后,我發現了這個問題,它幫助我找到了答案。
我需要做的就是將AllowOverride All
添加到我的httpd-vhosts.conf
文件中,如下所示:
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents/site/public"
ServerName testurl
<Directory /Library/WebServer/Documents/site/public>
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
這糾正了問題,現在一切正常加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.