由于 apache2 配置,Laravel 4 控制器路由不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17263458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 controller routes not working due to apache2 configuration
提问by Gavin Sellers
I'm trying to set up laravel on my local server at home and I can't seem to get controller routes working eg:
我正在尝试在家里的本地服务器上设置 laravel,但似乎无法使控制器路由正常工作,例如:
Route::controller('home','home');
This code works on a server I have at work and I've read a few forums about the issue which claim the issue is because apache2 isn't configured to look at the .htaccess files.
这段代码在我工作的服务器上工作,我已经阅读了一些关于这个问题的论坛,这些论坛声称这个问题是因为 apache2 没有配置为查看 .htaccess 文件。
I've been messing around with that for a while but I still can't seem to get anything to work. I went into /etc/apache2/sites-available and created my own config file called site1 which looks like this:
我一直在搞这个,但我似乎仍然无法得到任何工作。我进入 /etc/apache2/sites-available 并创建了我自己的名为 site1 的配置文件,如下所示:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/laravel/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/laravel/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
I then ran a2dissite default && a2ensite site1 and /etc/init.d/apache2 restart but nothing happened.
然后我运行 a2dissite default && a2ensite site1 和 /etc/init.d/apache2 restart 但什么也没发生。
Thanks for your help and let me know if I need to clarify anything.
感谢您的帮助,如果我需要澄清任何事情,请告诉我。
Turns out apache isn't even reading the .htaccess file. I purposely wrote some nonsense in it that should have caused an error and it didn't.
结果 apache 甚至没有读取 .htaccess 文件。我故意在里面写了一些应该导致错误的废话,但它没有。
回答by Gavin Sellers
The issue turned out to be that mod_rewrite.c wasn't enabled, so I ran a2enmod rewrite and everything started working. I noticed that the .htaccess file in laravel was dependent on it. Silly me. Thanks for all the help.
问题原来是 mod_rewrite.c 没有启用,所以我运行了 a2enmod rewrite,一切都开始工作了。我注意到 laravel 中的 .htaccess 文件依赖于它。傻我。感谢所有的帮助。
回答by saran banerjee
In the .htaccess try replacing this line:
在 .htaccess 中尝试替换这一行:
RewriteRule ^ index.php [L]
with
和
RewriteRule ^ /index.php [L]
EDIT:
编辑:
In that case I think that your virtual host is not set in the httpd.conf. You can try this out and see whether it works or not:
在这种情况下,我认为您的虚拟主机未在 httpd.conf 中设置。你可以试试这个,看看它是否有效:
Put this line in the httpd.conf file
将此行放在 httpd.conf 文件中
For windows
窗户用
NameVirtualHost *:80
<VirtualHost *:80>
ServerName yourlaravel.com
DocumentRoot "C:/wamp/www/laravel/public"
<Directory "C:/wamp/www/laravel/public">
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/wamp/www"
<Directory "C:/wamp/www">
</Directory>
</VirtualHost>
For Linux
对于 Linux
NameVirtualHost *:80
<VirtualHost *:80>
ServerName yourlaravel.com
DocumentRoot "/var/www/laravel/public"
<Directory "/var/www/laravel/public">
</Directory>
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/var/www"
<Directory "/var/www">
</Directory>
</VirtualHost>
And to run it in your local machine
并在您的本地机器上运行它
For window open the C:\Windows\System32\drivers\etc\hosts put this line.
对于窗口打开 C:\Windows\System32\drivers\etc\hosts 放这一行。
yourserverip yourlaravel.com
For linux open the \etc\hosts put this line.
对于 linux 打开 \etc\hosts 放这一行。
yourserverip yourlaravel.com
Your can refer to this link for further info:
您可以参考此链接了解更多信息:
http://net.tutsplus.com/tutorials/php/building-web-applications-from-scratch-with-laravel/
http://net.tutsplus.com/tutorials/php/building-web-applications-from-scratch-with-laravel/
I hope this can be some help.
我希望这可以有所帮助。
回答by Alexandre Butynski
This kind of error often appears when your public/.htaccess
is not correct. Try this one :
当你public/.htaccess
的不正确时,经常会出现这种错误。试试这个:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
回答by clement
If you are uploading to AWS EC2, use default /var/www/html
as the root directory:
如果您要上传到 AWS EC2,请使用 default/var/www/html
作为根目录:
[ec2-user@www html]$ pwd
/var/www/html
[ec2-user@www html]$ ll
-rw-r--r-- 1 ec2-user ec2-user 146 Nov 10 06:29 CONTRIBUTING.md
drwxr-xr-x 12 ec2-user ec2-user 4096 Mar 8 16:21 app
-rwxr-xr-x 1 ec2-user ec2-user 2452 Nov 10 06:29 artisan
drwxr-xr-x 2 ec2-user ec2-user 4096 Mar 5 00:18 bootstrap
-rw-r--r-- 1 ec2-user ec2-user 717 Nov 10 06:29 composer.json
-rw-r--r-- 1 ec2-user ec2-user 58819 Jan 11 22:00 composer.lock
drwxr-xr-x 3 ec2-user ec2-user 4096 Jan 11 22:24 nbproject
-rw-r--r-- 1 ec2-user ec2-user 567 Nov 10 06:29 phpunit.xml
drwxr-xr-x 3 ec2-user ec2-user 4096 Nov 10 06:29 public
-rw-r--r-- 1 ec2-user ec2-user 2051 Nov 10 06:29 readme.md
-rw-r--r-- 1 ec2-user ec2-user 519 Nov 10 06:29 server.php
drwxr-xr-x 20 ec2-user ec2-user 4096 Jan 11 22:00 vendor
Keep ec2-user as the owner of all the laravel files. Execute:
保留 ec2-user 作为所有 Laravel 文件的所有者。执行:
$ sudo chmod -R 777 app/storage
$ sudo php artisan serve --host 0.0.0.0 --port 80
https.conf:
https.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/var/www/html/"
<Directory "/var/www/html/">
Options Indexes FollowSymLinks Includes execCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
/var/www/html/.htaccess:
/var/www/html/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>