Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to serve my Laravel application on my local apache web server. However, I am having issues.

To test if the application I made would work on an apache server, I have created a new very simple application which contains two routes.

Route::group(['middleware' => 'web'], function() {
    Route::get('/', function() { return view('myview'); });
    Route::get('/link', function() { return view('myotherview'); });
});

When I enter my public directory from my browser, it works fine, it connects to the / route. But when I give a link to the other route (/link), and try enter that route, it gives me 404 not found the error. Here is the link I give in my myview view to reaching /link route:

<a href="{{ url('/link') }}">Go</a>

When I show the source of the page, the above line is rendered as localhost/mylaravelapp/public/link.

I have researched this issue on the internet and there are a couple of suggestions on enabling apache mod_rewrite. I have also done that by typing a2enmod mod_rewrite. However this isn't seemed to be working, getting the same result. How can I solve this issue?

My laravel version is 5.2, apache 2.4.7 and I am using xubuntu 14.04.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
359 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is the usual configuration that works for me (same OS, Apache and Laravel version as you have). Edit the apache2 config file (it should be under /etc/apache2/sites-available/000-default.conf), adding this:

Alias /yourdir /var/www/html/yourdir/public/
<Directory "/var/www/html/yourdir/public">
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>

Where "yourdir" is obviously your folder under the /var/www/html path. Restart your server after you modified the config file

sudo service apache2 restart

Now your public/.htaccess should be something like:

RewriteEngine On
RewriteBase /yourdir/

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Authorization Headers
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...