2014-07-16 243 views
10

例子:Laravel路由不工作,Apache的配置只允許公共/ index.php文件/路由

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

要查看上述路線,我必須導航到公共/ index.php文件/得到。

我已經瀏覽了很多SO帖子並圍繞嘗試不同的東西進行了搜索,並沒有改變(是的,我每次都重啓apache)。

這是我在公共目錄的.htaccess:

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks 
    RewriteEngine On 
</IfModule> 

# For all files not found in the file system, reroute the request to the 
# "index.php" front controller, keeping the query string intact 

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

什麼可能會導致這還是?我正在運行Ubuntu。

+0

我只是將'root/public/.htaccess移動到root/.htaccess'並且它工作 –

回答

12

您是否在apache上安裝並啓用了mod_rewrite?嘗試刪除if行(和),並在嘗試加載網站時查看它是否引發錯誤。如果是,請運行sudo a2enmod rewrite並重新啓動apache。

這是的.htaccess我有我的公開/目錄:該行爲

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

    RewriteEngine On 

    # 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

sudo a2enmod重寫 perl:warning:設置區域設置失敗。 perl的:警告:請檢查您的區域設置: \t LANGUAGE =(未設置), \t LC_ALL =(未設置), \t LANG = 「是en_US.UTF-8」 支持,並安裝在系統上。 perl:警告:回退到標準區域設置(「C」)。 模塊重寫已啓用 我不確定是否啓用了它...我可以在哪裏做到這一點? – user3817533

+0

它似乎已安裝並啓用。 「模塊重寫已啓用」。你可以嘗試使用我的.htaccess? –

+0

您的.htaccess取得了訣竅。我很感激。 – user3817533

37

兩個最常見的原因是:未啓用

  1. mod_rewrite的

    sudo a2enmod rewrite && sudo service apache2 restart

  2. AllowOverride被設置爲無,將其設置爲所有,假設Apache2.4

    sudo nano /etc/apache2/apache2.conf

搜索<Directory /var/www/>,改變AllowOverride NoneAllowOverride All,然後保存文件並重新啓動Apache

+0

在我的apache2.conf中沒有。 ls/etc/apache2: root @ none:/ etc/apache2#ls apache2.conf conf.d mods-available sites-enabled apache2.conf.dpkg-dist envvars mods-enabled conf-available httpd .conf ports.conf conf-enabled magic sites-available root @ none:/ etc/apache2# – user3817533

+0

是的,它在apache2.conf文件中。但你的問題已經解決了。祝你今天愉快! – delmadord

+0

我只是啓用rewrite_module並像魅力一樣工作! – Memonic

0

如果你不這樣做使用.htaccess文件,您需要指定您的重定向目錄:

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    <Directory /home/vitela/laravel/public> 
     RewriteBase /public 
     RewriteRule ^(.*)/$ /$1 [L,R=301] 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ /index.php [L,QSA] 
    </Directory> 
    </IfModule> 
0

在MacOs中。 .htaccess的主要問題不起作用,在Apache配置的httpd.conf文件中沒有啓用mod_rewrite.so。 我已經通過取消對線解決了這個:

的LoadModule rewrite_module的libexec/apache2的/ mod_rewrite.so

從上方線httpdf.conf卸下#。然後它會起作用。請享用!

0

AllowOverride None更改爲AllowOverride Allsudo a2enmod rewrite && sudo /etc/init.d/apache2 reload確實有幫助。