2012-11-22 96 views
46

試試我使用作曲家安裝的laravel 4。已經建立了一個虛擬主機。只有root路由工作:laravel 4除家外的所有路線導致404錯誤

Route::get('/', function() 
{ 
    return View::make('hello'); 
}); 

即使這是行不通的

Route::get('/hello', function() 
{ 
    return View::make('hello'); 
}); 

我試圖打一個TasksController/tasks

Route::resource('tasks', 'TasksController'); 

這是給我404太。

我能做什麼錯?我跟着這些指示,而安裝:http://chris-schmitz.com/getting-started-with-laravel-4/

而且我有我的項目的根目錄默認.htaccess文件:使用本地主機的MAC

<IfModule mod_rewrite.c> 
    Options -MultiViews 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
</IfModule> 

我。

+6

艾米特:你解決你的問題?如果是的話,請發佈您的解決方案,因爲我面臨着與我的laravel項目相同的問題。在此先感謝 – 2619

+0

@AmitErandole哎呀。我們似乎錯過了您發佈的gist.github.com/4131966的要點。 – itsazzad

回答

6

定義以外的任何時候比的家,你並不需要一個/:

Route::get('hello', function() 
{ 
    return View::make('hello'); 
}); 

應該工作。

+7

試過了。也行不通 - 所有的道路導致404 –

93

只是爲了歡笑,看看/index.php/hello是否有效。

如果是這樣,那很有可能是.htaccess問題。

+1

廢話! /index.php/hello工作,但/index.php/tasks現在給我一個500錯誤。 WTF?我將更新我的問題與htaccess的內容 –

+0

我不明白:我有mod重寫和AllowOverride設置爲全部 –

+2

檢查* .htaccess中的*任何*工作 - 如果您運行Apache 2.4,請注意自上一版本以來所做的更改,關於您的虛擬主機上的' ...'塊中的'要求全部授予'和'AllowOverride all'。 並重新啓動Apache運氣。 – GaryJ

4

由於Laravel 4自動加載靜態文件中地圖的文件,因此您需要在添加新控制器時更新該文件。運行此命令重建靜態文件:

php composer.phar dump-autoload 
36

在WAMP(Windows 8)上運行Laravel 4時出現同樣的問題。
爲我工作的解決方案是:

  1. 打開Apache的httpd.conf找到這一行:

    #LoadModule rewrite_module modules/mod_rewrite.so 
    
  2. 取消註釋該行(刪除#
  3. 保存httpd.conf
  4. 重啓WAMP

它應該工作!

+0

我沒有#LoadModule rewrite_module模塊/ mod_rewrite.so在我的httpd.conf :(是一個Windows8的東西嗎?我在Windows7上使用XAMPP – marienke

+12

在Ubuntu上,這將是:'sudo a2enmod rewrite'緊接着' sudo service apache2 restart' –

+0

Thanks!This works in my Wamp Server(Windows 8.1) –

6

即使在啓用mod_rewrite之後,如果您正在使用laravel /公用文件夾進行別名,則可能會遇到此問題。

添加

RewriteBase /your_apache_alias 

爲.htaccess的伎倆。

+1

我正在使用別名,我。 – nmoliveira

+0

我有同樣的問題,使用Ubuntu服務器14.04。在apache2.conf中使用了別名,mod_alias已經啓用。在公用文件夾中添加rewritebase文件.htaccess確實解決了這個問題。謝謝! – Frendhi

+0

謝謝,這就是它! – Mirko

3

我有這個問題(在Windows上手動安裝Apache 2。2),原因是我的VirtualHost中缺少AllowOverride,因爲httpd.conf中的根目錄默認爲None。

FileInfo Options=MultiViews是最少的一組,你需要爲Laravel的.htaccess

例如

<VirtualHost *:80> 
    DocumentRoot "C:/htdocs/domain.com/laravel/public" 
    ServerName foo.domain.com 

    <Directory "C:/htdocs/domain.com/laravel/public"> 
     AllowOverride FileInfo Options=MultiViews 
    </Directory> 
</VirtualHost> 

或者使用AllowOverride All如果這不起作用,你不關心安全性。

26

我有同樣的問題和解決辦法是啓用的Apache2

重寫國防部在終端使用以下命令:

$ sudo a2enmod rewrite 
$ sudo service apache2 restart 

和魔法!

+0

你和@GaryJ救了我的一天!謝謝 – markov00

+0

它適用於我。 +1 –

2

這就像@GaryJ & @MartinGomez說。這是的.htaccess應該在公共文件夾,Apache服務器上運行的所有laravel 4個項目中設置的內容:

<IfModule mod_rewrite.c> 
    <IfModule mod_negotiation.c> 
     Options -MultiViews 
    </IfModule> 

    RewriteEngine On 
    RewriteBase/

    # Redirect Trailing Slashes... 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
</IfModule> 
0

此更新爲我工作。在.htaccess文件中,只需在Rewriterule打開後添加以下內容。

Options -Indexes 
Options +FollowSymLinks 

工作就像魅力

2

有在前面的回答有點小瑕疵。 這可能有幫助。

$ sudo a2enmod rewrite 
$ sudo service apache2 restart 

希望它能做到。

13

有完全相同的問題。

兩件事情我需要做的,以解決這個問題:

  1. 從無到所有,例如(阿帕奇2.4.9)使rewrite_module在Apache中
  2. 更改的AllowOverride

    選項索引FollowSymLinks MultiViews AllowOverride全部與VirtualBox和流浪,而不是WAMP 使用Laravel家園在一起:要求所有授予

僅供參考。它包含Nginx而不是Apache,但是默認情況下,它都可以工作,因爲它明確地爲Laravel配置。

+0

這在我的服務器上部署時適用於我。 Vagrant是一個很好的建議,更明確地說我會建議http://laravel.com/docs/4.2/homestead或https://box.scotch.io/如果你想堅持到Apache – Luca

+0

AllowOverride爲我工作新鮮xampp安裝和虛擬主機 –

+0

我有完全相同的問題。我還爲apache執行了「sudo a2enmod rewrite」命令,然後在apache2.conf文件中將AllOverride從None更改爲All。 –

3

這很明顯,問題出現在「mod_rewrite」中,在某些情況下,只需在Apache中啓用該模塊就足以使其得到修復。

然而,在我的情況,我不得不延長配置的虛擬主機以下列方式:

<VirtualHost *:80> 

    ServerName adplus.local 

    ServerAdmin [email protected] 
    DocumentRoot /var/www/adplus.local/project/public 

    **<Directory "/var/www/adplus.local/project/public"> 
     AllowOverride All 
    </Directory>** 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 
3

我嘗試了所有這些沒有成功,然後我發現了另一篇文章,並改變了我的.htaccess這個:

<IfModule mod_rewrite.c> 
Options -MultiViews 
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^/index.php [L] 
</IfModule> 
+0

完美。其實我忘了在我的根目錄下複製.htaccess –

0

此評論可能有點晚,但可能有所幫助。

Route::get('index', function() { 
return view('index'); 
}); 

Route::get('login', function() { 
return view('login'); 
}); 

但沒有工作,所以我嘗試了一切,我在所有相關的帖子中發現,但並不足以解決我的問題,所以我發現這行:路由到這樣的另一種觀點認爲,當我有同樣的問題在httpd.conf中:

<Files ".ht*"> 
Require all denied 
</Files> 

,所以我改變「拒絕」到「授」,也有評論對我的.htaccess這一行:

#RewriteBase/

而工作!我認爲這種做法使得Apache不考慮項目的.htaccess文件,因此將它變爲理所當然。 希望這可以幫助有同樣問題的人。

Apache服務器2的工作@ Manjaro Linux操作系統(基於的Archlinux)

1

對於Nginx的用戶,你可能複製的default虛擬主機 - 但Laravel需要對location指令一些定製,讓Laravel應用做路由而不是Nginx。

在你/etc/nginx/sites-available/your-site-name這個替代的位置指令:

location/{ 
    try_files $uri $uri/ /index.php$is_args$args; 
} 

然後運行sudo service nginx reload以使更改生效。

2

轉到

納米的/ etc/nginx的/網站可用/ yoursite

找到try_file和如下替換:

try_files $uri $uri/ /index.php?$query_string;

0

我有一個類似的問題在Ubuntu 14.04上使用Lumen 5.4。

解決方案對我來說是兩個部分。首先在我的虛擬主機文件/etc/apache2/sites-enabled/my_vhost.conf,我不得不添加以下<Directory>條目,從而允許覆蓋,宣告我的DocumentRoot後:

DocumentRoot /var/www/path/to/my/app/public 

    <Directory "/var/www/path/to/my/app/public"> 
      Options FollowSymLinks 
      AllowOverride All 

      Order allow,deny 
      Allow from all 
    </Directory> 

接下來,我不得不啓用與重寫:

sudo a2enmod rewrite 
sudo service apache2 restart