Laravel 5.1 - Dreamhost VPS - Always getting 404 exception - php

I have been breaking my head for the last two days now and still don't have a solution. All routes in my Laravel 5.1 are throwing a NotFoundHttpException.
My folder structure looks like this:
Some additional info
Both example.com and abc.example.com have web directory set to
/home/username/example.com/myproject/public
I have a .htaccess file inside the public folder which looks like
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]
As Image:
Debugbar is showing the following error:
Any help will be highly appreciated. I have tried almost all solutions found on Google. None worked.

So after fighting tooth and nail to resolve this, I decided to take a different approach and to my relief, IT WORKED!
Rather than deploying inside a "myproject" folder, I copied all Laravel files directly under example.com. So if you're cloning your repository (as I was), I manually copied files one level up.
Created the proper .env file
Ran the following commands in order:
composer dump-autoload
php artisan config:cache
and voila! things were up and running!
Now ... some other imporant things to note.
If you're using subdomain routing, you have to setup your domain such that it doesn't put the www. In my case, http://example.com was becoming http://www.example.com and my controller actions were failing. I am sure there is a work around for this. Will update this answer, if I find and answer.

Related

How to solve Laravel's Route 404 error on Login page?

A project, which is prepared with Laravel Framework, has handed over to me. Previous developer has not used git, some composer commands and Laravel's .gitignore file(also removed it). On the other hand he has prepared the project on the production server.
Anyway. Firstly I've found a gitignore for laravel and pushed the project to my repository. Then I wanted to pull it to my local. All folders and files pulled except of vendor folder.
Then I executed:
composer install
Gave me this error:
Class 'Maatwebsite\Excel\Excel' not found
Then I fixed it by the method here.
Then retried composer install, gave me this error:
Undefined index: name
Then fixed it by the method here.
Then retried composer install, and succeeded. Then I cleared cache with php artisan cache:clear
I've configured .env file and tried to enter to homepage. It redirected me to login page and gave 404. I've checked the .htaccess, there was redirection like that:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://example.com/app/$1 [R=301,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
<IfModule mime_module>
AddType application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Changed the example.com to my localhost. But still gives me 404.
Is there anything that I'm missing?
Note: Called the previous developer, and he said me "directly copy and paste the all folders". Hi from 90s :(
OP Said this worked for him so I'm moving it from comments to an answer:
Let's go over what should be there for the pages to return correctly: First you need a routes folder with with all the routes in files inside of it. Second you need the Controller folder (found in /app/Http). Third you need the resources folder with the actual views. Fourth you need the public folder with the CSS/JS, etc.
Since OP stated that all the views work except for the login view:
First find out what the login view page is called, then go to your routes and make sure there's a line such as
Route::get('/login', 'SomeController#somefunction')->name('login.path');
Then go to the controller that is referenced in that route, and make sure that the function returns the view of the login page
Have you tried to generate you laravel application key using php artisan key:generate.
If that also doesn't work then try to remove .htaccess temporary.
Try this command
composer require maatwebsite/excel:^3.0

Can't access Laravel /public anymore. Laravel handles it as a route.

In DirectAdmin, I have changed the domainname of my project. But unfortunately it did not gave me the result I was looking for. So I undid my action. But, that did not solve the problem.
At first, I was able to access my site.com/public folder to use the assets for my website. But after my failure Laravel handles the /public directory as a route. So, I dan't have access anymore to my public folder and needed assets.
My domain: good2beout.nl
Can anyone help me?
P.s. I already looked at question Laravel 4 - public directory being treated as a route - image not showing but this does not solve my problem.
I have found my problem.
I accidentally overwrote my .htaccess file, replaced it with
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
https://laravel.com/docs/4.2#pretty-urls

Laravel - How to remove index.php from Laravel in windows xampp

Problem:
my routes not working except the root home page, I'm searching for two days to find a solution to this problem and what I found that I should change .htaccess file but solutions didn't fix any for my case, at first the url localhost/quotes/public was working well with me, but at some point I'm not sure what is it this issue showed up
what I tried:
create another route and I made sure that no routes are working only
home route, still not working except home
tried to change OverrideMode on my XAMP from None to All, didn't fix any
tried to type manually localhost/quotes/public/index.php BOOM everything
works ..
my htaccess file:
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
working on:
Windows 10
XAMP
Laravel 5.2.35
The problem is that your .htaccess is rewriting everything to the frontcontroller, which is normally located at {host}/index.php. In your application however it is located at {host}/quotes/public/index.php.
So you have 2 options:
1. virtual host
Set up a virtual host in your XAMPP Apache that points ie. myapp.local to htdocs/quotes/public Here is an example of how to achieve this: how to create virtual host on XAMPP. (Don't forget to add the host to your hosts file and have it point to your local macine on 127.0.0.1) You can then access your application on myapp.local/whatever-route-you-define. Alternatively you forget about XAMMP and install the homestead virtual machine, which comes preconfigured for this.
2. rewrite rule
Change you rewrite rule to rewrite all requests to quotes/public/index.php in stead of index.php. I'm no htaccess expert, but I believe it should be as simple as changing this:
RewriteRule ^ index.php [L]
to this:
RewriteRule ^ quotes/public/index.php [L]
Do note that you'll still need to access your application trough localhost/quotes/public/whatever-route-you-define which is not ideal imo. Your dev version should be as close to your live version as possible, and if you start working with absolute and relative paths and stuff in your code things will become a mess sooner rather then later.
Personally I would go for Homestead, I use it all the time and it works great once you have it running.
Btw, the reason why localhost/quotes/public/index.php is working for you right now is because RewriteCond %{REQUEST_FILENAME} !-f tells Apache not to rewrite any requests to files that actually exist (otherwise you wouldn't be able to access static assets like your css).
The .htaccess file must be at the root of the application.
Add this in this file :
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Assuming you haven't touched the original architecture of Laravel, and that public data is still in the same place : the public/ folder
You can also follow this good tutorial
Let me give you an example of the way I have my routes setup.
In app\Http\routes.php, here are three sample routes that I have.
Route::get('/', function () {
$values = app('App\Http\Controllers\KeywordController')->index();
dd($values);
return view('welcome');
});
Route::get('googlefile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileGoogle();
dd($output);
});
Route::get('bingfile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileBing();
dd($output);
});
I have WAMP setup on my environment. I have made a controller at app\Http\Controllers\KeywordController.php. If my browser is set to localhost/googlefile, then it will goto the method printToFileGoogle() in KeywordController.php.
Please try something similar to this and tell me if you get an error and if you do what error you get.

Laravel not recognized after .htaccess changes

Here is the baffling sequence of events that just transpired.
1) I have a working Laravel installation, with an empty .htaccess file, all URLs work.
2) I add some rules to .htaccess just to play around a bit, then remove the rules.
3) Now I have an empty .htaccess again (so nothing has changed compared to step 1 above) but now no URLs work anymore except the homepage.
For example, you will see that http://jovansprojects.com/march_madness/ works (that's the root of the Laravel folder), using this route:
Route::get('/', 'HomeController#showWelcome');
But http://jovansprojects.com/march_madness/login does not work anymore, despite having worked normally before.
This is the relevant route:
Route::get('login', 'SessionsController#create');
Note that it doesn't throw a Laravel error, like a 'not found exception' - it simply doesn't seem to know Laravel is installed there at all.
I've tried clearing Artisan's cache according to some online suggestions but didn't help.
So, to sum up: .htaccess is exactly the same as it was before (empty) but it seems the server doesn't even know to ask Laravel to handle the URLs anymore, not beyond the homepage anyway.
These are the rules I added to, and then removed from, .htaccess (I wanted to redirect trailing slashes):
Options -MultiViews
RewriteEngine On
RewriteBase /march_madness/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I suspect that RewriteBase may have something to do with it, but after I deleted the rules why didn't it revert to the way it was?
It would seem that Laravel does need the "Front Controller" in .htaccess in order to be able to route "pretty" URLs (ie. without having to use index.php in the URL and rely on PATH_INFO).
So, at the minimum, something like the following would be required in the .htaccess file (probably in the root of your Laravel install):
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
It would seem that there were two .htaccess. Note that .htaccess files are inherited along the filesystem path. However, mod_rewrite (to which the directives above belong) is not inherited by default. So, if you enable the rewrite engine in a subdirectory then the mod_rewrite directives in the parent directory will be ignored.

Symfony working with multiple bundle and uploading bundle to server

I have two problems with symfony. First one is, if I have two bundles created, only the first created bundle is shown when I go to localhost/symfony/web/app_dev.php/ As of now, I have the two bundles in my src/ both bundles are registered in the kernel, routes added and everything. But, I can only see the first created bundle, I don't know what to do, to see the second bundle in action.
Second problem is that, when I want to upload my finished symfony project, I can't see know how. Like, do I tell .htaccess to treat the app_dev.php files as index files, and to hide the folder name symfony from the URL, so that the mysite.com/symfony/web/app_dev.php to show as mysite.com/index.php because, I don't really get it.
Any help is much appreciated.
To "see second bundle in action", point your browser to some route of such bundle.
To be sure its routes have been registered, run:
$ php app/console router:debug
and see if they appear in the list.
Regarding deploying, you should read this topic.
Keep in mind that app_dev.php is meant for development environment, and should not be used for production; included .htaccess already routes all requests to app.php.
In order to make http://www.mysite.com/ your entry point, you should /yourproject/web be your webroot, or better use a symlink.
As the .htaccess part you way copy this into the .htaccess and put it in the root folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# DEV ENVIRONMENT #
RewriteRule ^$ symfony/web/app_dev.php [QSA]
RewriteRule ^(.*)$ symfony/web/app_dev.php/$1 [QSA,L]
# PROD ENVIRONMENT #
RewriteRule ^$ symfony/web/app.php [QSA]
RewriteRule ^(.*)$ symfony/web/app.php/$1 [QSA,L]
</IfModule>

Categories