1

I'm deploying my first laravel app with laravel-localization. Unfortunately, I am currently getting the error,

Not Found

The requested URL /en was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I have searched the web trying to find others who had the same problem and see if they had a viable solution for me. I found at least one, but it looks like he wasn't able to solve it either. I also found this solution here, but neither idea seemed to work.

When I change (in the config/laravellocalization.php file)

'hideDefaultLocaleInURL' => false,

from False to True

'hideDefaultLocaleInURL' => true,

I can see the homepage, but only that. All other pages are redirected to the 404 error.

I have also changed the index.php file to reflect my folder structure as the public folder for this site is within a folder within the public_html folder:

require __DIR__.'/../../myapp/bootstrap/autoload.php';

...

$app = require_once __DIR__.'/../../myapp/bootstrap/app.php';

Note:1 Everything worked on my localhost. There seems to be a problem in deployment.

Note 2: As it is currently configured, the site automatically tries to load the www.mysite.com/en version, which makes me think that my changes to the index.php file were correct. Without these changes, I get the error that the page cannot load.

Note 3: Here is my current folder structure:
- /home/username
-- mostOfMyFiles
-- public_html
--- nameOfUrl
---- publicFiles

Note 4: Regarding routing, here is what I currently have in my web.php file:

<?php

    Route::group([
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
        ], function()
    {
        Route::get('/', function()
        {
            return View::make('welcome');
        });

        Route::get(LaravelLocalization::transRoute('routes.about'), function() {
            return View::make('about');
        });

        Route::get(LaravelLocalization::transRoute('routes.contact', 'ContactController@getContact'), function() {
            return View::make('contact');
        });

        Route::post(LaravelLocalization::transRoute('routes.contact'),
            ['as' => 'contact', 'uses' => 'ContactController@sendMail']);

    });

I have tried adding the 'web' middleware to the group, as seen here, but this didn't solve the problem either. :(

Note: I am also hosting another site. That is why I have the public files within a folder that has the name of the URL.

Any idea what the problem could be?

I appreciate all ideas, comments, and criticism. :)

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
  • 1
    Are you sure you're supposed to change the `index.php` file ? I don't think that is a good idea. There are usually variables that you have to change in the `config` files or the `.env` file – xperator Jul 14 '17 at 14:29
  • Good question @xperator. This is actually my first time deploying a Laravel site. So, I'm not really sure about that. I'm following this tutorial: https://www.youtube.com/watch?v=6g8G3YQtQt4 - which recommended changing the index.php file in the way that I did. – Brad Ahrens Jul 14 '17 at 14:32
  • You know the first thing that comes to my mind is to add a prefix to all your routes. I mean this would usually be the case if your Laravel is in a custom path. Here is a link I found about localization not sure if it's related though https://laracasts.com/discuss/channels/laravel/l5-localisation-routing – xperator Jul 14 '17 at 14:47
  • I'm a bit confused since in your Note 2 you're saying you're trying to serve the `mysite.com/en` URL. I thought you want to serve it under a subdirectory, like `mysite.com/my-laravel-app/en`. If you have Laravel in a subdirectory inside `public_html` and you want to serve it with your root path, then all you have to do is to config your web server and tell it to use the `/public_html/my-laravel-app` for the root path. – xperator Jul 14 '17 at 14:54
  • The changes you made in your `index.php` file right now is for when you want to serve Laravel from a sub-directory So that when you enter `mysite.com/my-app` in your browser, Laravel would run. It is not supposed to let Laravel run from `mysite.com` – xperator Jul 14 '17 at 14:59

3 Answers3

3

I had same problem.

After some deep research I found solution that work for me. I am a little bit noob as well with laravel deployment and I dont know why is this such a problem to deploy laravel project on shared hosting, there is literally thousand ways and every time someone said that this isn't the right way, so I dont even know what is actually right way.

I add this block of code in my "public_html/.htacces" Options -MultiViews

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]

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

Sven
  • 63
  • 1
  • 11
2

If someone's looking, in my case it was route:cache that caused 'Not found' exception. I completle forgot, that by default you can't use route:cache with LaravelLocalization.

jes490
  • 31
  • 1
  • 3
0

Everything worked on my localhost. There seems to be a problem in deployment.

Are you automatically using cache in deployment? For LaravelLocalization you need to use

php artisan route:trans:cache

instead of

php artisan route:cache

See docs for more.

Adam
  • 25,960
  • 22
  • 158
  • 247