繁体   English   中英

Laravel 5.2漂亮的URL

[英]Laravel 5.2 Pretty URLs

我如何才能将http://domain.com/public/index.php更改为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 ^(.*)/$ /$1 [L,R=301]

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

使用此设置,是的,我能够设置http://domain.com,但是当我尝试调用另一条路由时,得到404。其背后的原因是,如您所见,我已将我的根文件夹设置为上市。 因此,我的路线无法到达目的地(例如,由于我的控制器不在公用文件夹中,因此它们就像被定向到我的控制器的路线一样)。

解决方法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:列表结果为

+--------+----------+---------+------+---------+------------+
| Domain | Method   | URI     | Name | Action  | Middleware |
+--------+----------+---------+------+---------+------------+
|        | GET|HEAD | /       |      | Closure | web        |
|        | GET|HEAD | myroute |      | Closure | web        |
+--------+----------+---------+------+---------+------------+

您需要在Web服务器的配置文件中正确设置虚拟主机。 public目录设置为Laravel VH的根目录,然后重新启动Web服务器

对于Apache,您可以使用以下指令:

 DocumentRoot "/path_to_aravel_project/public" <Directory "/path_to_aravel_project/public"> 

对于nginx,您应该更改以下行:

 root /path_to_aravel_project/public; 

这就是我在项目中处理此问题的方式。 有两个步骤。

1.在/ 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 ^(.*)/$ /$1 [L,R=301]

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

2.在项目根目录中有一个名为server.php的文件(应用程序的父级,公共级等)。

将其重命名为index.php

它应该工作..没有任何麻烦。

是的,每个答案都可以正常工作,但是在此之前,我发现apache重写模块mod_rewrite应该已经被激活。

在ubuntu控制台上: a2enmod rewrite解决了我的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM