Laravel 5.2 漂亮的网址
Laravel 5.2 Pretty URLs
我如何更改 http://domain.com/public/index.php to http://domain.com 并使 ('/')
以外的其他路由正常工作?
解决方法 1:
虚拟主机文件:
<VirtualHost *:80>
DocumentRoot "/var/www/html/domain/public"
ServerName domain.com
<Directory "/var/www/html/domain/public">
AllowOverride All
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
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>
使用此设置,是的,我可以设置 http://domain.com 但是当我尝试调用另一条路线时,收到 404。如您所见,这背后的原因是我已经设置了我的根文件夹为 public。所以我的路线无法到达它们的目的地(比如那些被定向到我的控制器的路线,因为我的控制器不在 public 文件夹中)。
解决方法 2:
如果这次我将文档根目录和目录更改为 /var/www/html/domain/
,我将失去漂亮的 url,并且我只能通过输入 http://domain.com/public/index.php
.
来请求主页
请注意,我使用的是 ubuntu 14.04.
你有什么建议?
---更新---
路由示例:
Route::get('myroute', array(
'uses' => 'MyController@myMethod',
'as' => 'myroute'
));
---更新2---
php artisan route:list 结果是
+--------+----------+---------+------+---------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+---------+------+---------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | myroute | | Closure | web |
+--------+----------+---------+------+---------+------------+
您需要在网络服务器的配置文件中正确设置虚拟主机。设置 .
For Apache you can use these directives:
DocumentRoot "/path_to_aravel_project/public"
<Directory "/path_to_aravel_project/public">
For nginx, you should change this line:
root /path_to_aravel_project/public;
这就是我在我的项目中处理这个问题的方式。有两个步骤。
1.create 在您的 /public 目录中创建一个新的 .htaccess 文件,内容如下:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
2。在您的项目根目录中有一个名为 server.php 的文件..(应用程序的父级,public 等)。
将其重命名为 index.php
它应该可以工作.. 没有任何麻烦。
是的,每个答案都很好用,但在此之前我发现 apache 重写模块 mod_rewrite 应该已经被激活了。
在 ubuntu 控制台上:a2enmod rewrite
解决了我的情况。
我如何更改 http://domain.com/public/index.php to http://domain.com 并使 ('/')
以外的其他路由正常工作?
解决方法 1:
虚拟主机文件:
<VirtualHost *:80>
DocumentRoot "/var/www/html/domain/public"
ServerName domain.com
<Directory "/var/www/html/domain/public">
AllowOverride All
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
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>
使用此设置,是的,我可以设置 http://domain.com 但是当我尝试调用另一条路线时,收到 404。如您所见,这背后的原因是我已经设置了我的根文件夹为 public。所以我的路线无法到达它们的目的地(比如那些被定向到我的控制器的路线,因为我的控制器不在 public 文件夹中)。
解决方法 2:
如果这次我将文档根目录和目录更改为 /var/www/html/domain/
,我将失去漂亮的 url,并且我只能通过输入 http://domain.com/public/index.php
.
请注意,我使用的是 ubuntu 14.04.
你有什么建议?
---更新---
路由示例:
Route::get('myroute', array(
'uses' => 'MyController@myMethod',
'as' => 'myroute'
));
---更新2--- php artisan route:list 结果是
+--------+----------+---------+------+---------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+---------+------+---------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | myroute | | Closure | web |
+--------+----------+---------+------+---------+------------+
您需要在网络服务器的配置文件中正确设置虚拟主机。设置
For Apache you can use these directives:
DocumentRoot "/path_to_aravel_project/public" <Directory "/path_to_aravel_project/public">
For nginx, you should change this line:
root /path_to_aravel_project/public;
这就是我在我的项目中处理这个问题的方式。有两个步骤。
1.create 在您的 /public 目录中创建一个新的 .htaccess 文件,内容如下:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
2。在您的项目根目录中有一个名为 server.php 的文件..(应用程序的父级,public 等)。
将其重命名为 index.php
它应该可以工作.. 没有任何麻烦。
是的,每个答案都很好用,但在此之前我发现 apache 重写模块 mod_rewrite 应该已经被激活了。
在 ubuntu 控制台上:a2enmod rewrite
解决了我的情况。