-->

Laravel 5.2 Pretty URLs

2019-07-14 20:04发布

问题:

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

Workaround 1:

vhost file:

<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 file:

<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>

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).

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.

Note that I'm using ubuntu 14.04.

What do you suggest?

---update---
Route example:

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

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

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

回答1:

You need to correctly setup virtual host in you web server's config file. Set public directory as root directory for Laravel VH and restart web server.

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;


回答2:

This is how I deal with this in my projects. There are two steps.

1.create a new .htaccess file in your /public directory with following content:

<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. there is a file named server.php in your project root.. (parent of app, public etc.).

rename that to index.php

And it should work.. without any hassle.



回答3:

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

On ubuntu console: a2enmod rewrite solved my situation.