本文介绍了Laravel 5.2漂亮的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 http://domain.com/public/index.php 更改为 http://domain.com 并可以获得除('/')之外的其他路由?

How can i change http://domain.com/public/index.php to http://domain.com and can get the other routes working other than ('/') ?

解决方法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.其背后的原因是,您可以看到我将我的根文件夹设置为public.因此,我的路线无法到达目的地(例如,因为我的控制器不在公用文件夹中,所以它们就像被定向到我的控制器的目的地一样).

With this setup, yes i'm able to set http://domain.com but when i'm trying to invoke another route, getting a 404. The reason behind this is as you can see i've set my root folder as public. So my routes cannot reach their destinations (like the ones which are being directed to my controllers, because my controllers are not in the public folder).

解决方法2:
如果这次我将文档根目录和目录更改为/var/www/html/domain/,则我将丢失漂亮的url,这是我只能通过输入http://domain.com/public/index.php来请求主页的唯一方法.

Workaround 2:
If i change Document root and directory into /var/www/html/domain/ this time i'm losing my pretty url and only way i can request main page by entering http://domain.com/public/index.php.

请注意,我正在使用ubuntu 14.04.

Note that I'm using ubuntu 14.04.

您有什么建议?

-更新----
路线示例:

---update---
Route example:

Route::get('myroute', array(
   'uses' => 'MyController@myMethod',
   'as' => 'myroute'
));

-更新2 ---php artisan route:列表结果为

---update 2---php artisan route:list results are

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

推荐答案

是的,每个答案都很好,但是在此之前,我发现apache重写模块 mod_rewrite 应该已经被激活.

Yes, each of those answers are nicely working, but before that I found out apache rewrite module mod_rewrite should have already been activated.

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

On ubuntu console: a2enmod rewrite solved my situation.

这篇关于Laravel 5.2漂亮的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:59