Laravel 5 需要使用 Apache 虚拟主机的 index.php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30825221/
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 5 needs index.php using Apache virtual host
提问by Sven van den Boogaart
When I load pages that are not root, the page won't load without index.php before the route. The error I get:
当我加载不是 root 的页面时,在路由之前没有 index.php 将无法加载页面。我得到的错误:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at mydomain.com Port 80
To start with I have a virtual host file containing:
首先,我有一个虚拟主机文件,其中包含:
//also tried adding DirectoryIndex here before <directory>
<directory /var/www/mydomain.com>
DirectoryIndex index.php
AllowOverride ALL
</directory>
and a .htacces in my public with :
和一个 .htaccess 在我的公共场合:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have another domain with the same .htaccess and appache config on the same server and it works fine. Also I did restart apache. If I use phpinfo on my index.php/route page I see at Loaded Modules:
我在同一台服务器上有另一个具有相同 .htaccess 和 appache 配置的域,它工作正常。我也确实重新启动了apache。如果我在 index.php/route 页面上使用 phpinfo,我会在 Loaded Modules 中看到:
mod_rewrite mod_setenvif
When running the website localy with xampp everything works fine. For hours i'm trying now but I can't fix it or find any error (logs are empty) or any solutions that I haven't tried yet.
使用 xampp 在本地运行网站时,一切正常。我现在正在尝试几个小时,但我无法修复它或找到任何错误(日志为空)或我尚未尝试过的任何解决方案。
Edit:
编辑:
Im using Ubuntu Ubuntu 14.04 x64 on a digital ocean VPS. And I tried turning it on and off again (as suggested). PHP Version 5.5.9-1ubuntu4.9. I followed thistutorial to config everything (except the direcoty part). I changed the location of the apache log and a file error.log is created on the given directory but no errors are in it.
我在数字海洋 VPS 上使用 Ubuntu Ubuntu 14.04 x64。我尝试再次打开和关闭它(如建议的那样)。PHP 版本 5.5.9-1ubuntu4.9。我按照本教程配置了所有内容(除了直接部分)。我更改了 apache 日志的位置,并在给定目录上创建了一个文件 error.log,但其中没有错误。
My currently appache config is :(i've triple checked the white parts where the domain name is).
我目前的 appache 配置是:(我已经三次检查了域名所在的白色部分)。
When I run apache2ctl -t D DUMP_VHOSTS I get
当我运行 apache2ctl -t D DUMP_VHOSTS 我得到
this looks fine to me, also tried disabling the default config but it didnt help.
这对我来说看起来不错,还尝试禁用默认配置,但没有帮助。
Note: i've replaced my real domain with mydomain.com in reality I use my real domain on these spots.
注意:我已经用 mydomain.com 替换了我的真实域名,实际上我在这些地方使用了我的真实域名。
Thoughthow do I know for sure that the conf file im editing is the one being used by the domain?
想我如何确定我正在编辑的 conf 文件是域正在使用的文件?
采纳答案by Sven van den Boogaart
I ended up deleting the complete conf file and copied it again from the one thats working. I took the default .htaccess from laravel and it worked. Unfortunately I still dont know what was wrong though. The current conf file looks like
我最终删除了完整的 conf 文件,并从正在工作的文件中再次复制了它。我从 laravel 获取了默认的 .htaccess 并且它起作用了。不幸的是,我仍然不知道出了什么问题。当前的 conf 文件看起来像
<Directory /var/www/mydomain.com>
AllowOverride ALL
DirectoryIndex index.php
</Directory>
My .htaccess in /public
我在 /public 中的 .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
回答by Neo
This is the correctAlternative htaccess rule for Laravel 5 suggested to use when the default doesn't work:
这是Laravel 5的正确替代 htaccess 规则,建议在默认值不起作用时使用:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The Options +FollowSymLinks
I believe plays an important role because the DirectotyIndex
acts like a symlink.
的Options +FollowSymLinks
,因为我相信,起着重要的作用DirectotyIndex
就像一个符号链接。
Also check /app/config/app.php to make sure you haven't set the Config('app.url') to contain the index.php.
还要检查 /app/config/app.php 以确保您没有将 Config('app.url') 设置为包含 index.php。
If you have laravel installed in a sub-directory see this: How can I remove "public/index.php" in the url generated laravel?
如果您在子目录中安装了 laravel,请参阅: 如何在生成的 laravel url 中删除“public/index.php”?
回答by CenterOrbit
Have you tried turning it all off (shutdown), and then turning it on again?
您是否尝试将其全部关闭(关闭),然后再次打开?
If everything is as you say, it should work. I would try a restart to see if it changes anything.
如果一切都如您所说,它应该可以工作。我会尝试重新启动,看看它是否有任何改变。
If it doesn't, please update with what OS you are using.
如果没有,请更新您正在使用的操作系统。
回答by Ben Harvey
I had almost exactly the same problem today, which @dasper also helped me out on. I am using digital ocean Ubuntu 14.04 also, and I had to run the command a2enmod rewrite
then reload apache2. It seemed that even though rewrite appearedto be on, it actually wasn't.
我今天遇到了几乎完全相同的问题,@dasper 也帮助了我。我也在使用数字海洋 Ubuntu 14.04,我必须运行命令a2enmod rewrite
然后重新加载 apache2。看起来,尽管重写似乎在进行,但实际上并没有。
回答by dasper
First, make sure mode_rewrite is enabled. I am sure you have probably done this but I want to make sure nothing is missed.
首先,确保 mode_rewrite 已启用。我相信你可能已经这样做了,但我想确保没有遗漏任何东西。
Next, see what version of apache you are running since the syntax has changed between 2.2 and 2.4. This should not be a big deal and highly doubt this is the culprit but could save you trouble in the future
接下来,查看您正在运行的 apache 版本,因为语法在 2.2 和 2.4 之间发生了变化。这应该不是什么大问题,并且高度怀疑这是罪魁祸首,但可以在将来为您省去麻烦
Next, try putting the rewrite condition inside of the vhost information instead of the htaccess file. This can help a little with performance as well as give you a console warn/err when restarting apache where the htaccess errors could be squelched.
接下来,尝试将重写条件放在 vhost 信息而不是 htaccess 文件中。这可以帮助提高性能,并在重新启动 apache 时为您提供控制台警告/错误,其中 htaccess 错误可能会被压制。
<directory /var/www/mydomain.com>
Options -MultiViews
AllowOverride ALL
RewriteEngine On
RewriteRule ^(.*)/$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</directory>
Beyond this point I would be at a loss without some more logging or information from the server. You can also add CustomLog
into the vhost and report all requests processed by the server.
除此之外,如果没有来自服务器的更多日志记录或信息,我将不知所措。您还可以添加CustomLog
到 vhost 并报告服务器处理的所有请求。
回答by user2479930
The problem could be with your virtual host configurationand/or a misplaced .htaccessfile.
问题可能与您的虚拟主机配置和/或放错位置的.htaccess文件有关。
Make sure your .htaccessfile is in the public/folder and that the DocumentRootis set to that public/ folder.
确保您的.htaccess文件位于public/文件夹中,并且DocumentRoot设置为该 public/ 文件夹。
And also, the < Directory>directive isn't controlling your virtual host but rather controls the folder itself and how it can be (or can't be) accessed on the server. To change the configuration of the virtual host do so inside the < VirtualHost>directive.
而且,< Directory>指令不控制您的虚拟主机,而是控制文件夹本身以及如何在服务器上访问它(或不能)。要更改虚拟主机的配置,请在< VirtualHost>指令中执行此操作。
Here is an example of a virtual host configuration:
下面是一个虚拟主机配置的例子:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/var/www/mydomain.com/public"
DirectoryIndex index.php
</VirtualHost>
Here is the default Laravel Apache config:
这是默认的 Laravel Apache 配置:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and here is an alternative:
这是一个替代方案:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
回答by Alex Kyriakidis
回答by BrokenBinary
I believe your directory path is wrong. You need /public
on the end because Laravel treats that directory as the web root. Try this:
我相信你的目录路径是错误的。最后你需要/public
,因为 Laravel 将该目录视为 Web 根目录。尝试这个:
<directory /var/www/mydomain.com/public>
DirectoryIndex index.php
AllowOverride ALL
</directory>
But the better solution is to put the contents of the .htaccess
file in your virtual host. Like this:
但更好的解决方案是将.htaccess
文件内容放在您的虚拟主机中。像这样:
<VirtualHost *:80>
DocumentRoot "/var/www/mydomain.com/public"
ServerName mydomain.com
ServerAlias *.mydomain.com
<Directory "/var/www/mydomain.com/public">
# Ignore the .htaccess file in this directory
AllowOverride None
# Make pretty URLs
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
Also, you should make sure your config file is loaded by running:
此外,您应该确保您的配置文件通过运行加载:
apache2ctl -t -D DUMP_VHOSTS
apache2ctl -t -D DUMP_VHOSTS
This will print a list of all the loaded vhosts. If yours isn't in that list then make sure it is enabled by running:
这将打印所有加载的虚拟主机的列表。如果您的不在该列表中,请确保通过运行启用它:
a2ensite [name_of_your_config_file]
a2ensite [name_of_your_config_file]
Then restart apache with:
然后使用以下命令重新启动 apache:
service apache2 reload
service apache2 reload
回答by vivoconunxino
One more note: don't forget to edit /etc/hosts
and add 127.0.0.2 yourdomain.com
再注意一点:不要忘记编辑/etc/hosts
和添加127.0.0.2 yourdomain.com