Laravel route directing to Home instead of throwing a NotFoundHttpException - php

I'm experiencing some unexpected behaviour in my laravel (4.2) routes.
Let's say my server is available at https://example.com. If I enter https://example.com/whatever/index.php I would expect laravel to throw a NotFoundHttpException, because a route to "whatever" is not defined. Instead laravel shows me the start page, indicating that my "home" route was catched.
If I solely enter https://example.com/whatever everything is fine (i.e. I get the NotFoundHttpException as expected). I neither have the problem on my localhost. Here https://localhost/laravel/whatever/index.php throws the NotFoundHttpException as expected.
My routes.php file:
// Home
Route::get('/', array( 'as' => 'home', function() {
return View::make('home');
}));
Maybe someone can give me a hint where to start searching what's causing that behaviour: Apache config, PHP config, Laravel config?
Ammendment as answer to S. Safdar:
At first I thought of a .htaccess redirect issue, too. In laravel's public folder (the web servers root dir) lays a .htaccess as follows:
<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>
As you can see, the redirect here is handled correctly. The requested "whatever/index.php" is no real file therefore it is redirected to be handled by laravels index.php. If I remove the Rewrite Conditions (plus Rule) I get a regular apache 404 error page. In my case that's of no help as I want laravel to correctly(!) handle all error pages. But for some reason laravels home route matches every url ending on /index.php.

It seems this issue is present with both nginx and apache servers. A few steps I took to mitigate the issue with apache:
Change the index.php filename:
public/index.php to public/start.php
Change .htaccess to read:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ start.php [L]
Change the reference to index.php in the server.php file to have:
require_once $paths['public'].'/start.php';

why not you your route use this like?
// Home
Route::get('/', function() {
return View::make('home');
}));
i think problem is coming from as that you used in your route array.

Related

Laravel new project routing not working?

Laravel new project routing not working? This is the routes.php file:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('ID/{id}',function($id){
echo 'ID: '.$id;
});
Route::get('/user/{name?}',function($name = 'Virat Gandhi'){
echo "Name: ".$name;
});
So what i have done is this. I started the local laravel development server with: php artisan serve. Just like the book told me that i am going through (Laravel 5). But now only the first routing works '/' which uses the welcome view blade template.
But all other routings don't work >.<
Can someone please help me? I'm stuck.
My app/public/htacess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# 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>
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
You have to use return not echo
Like this:
Route::get('ID/{id}', function($id) {
return 'ID: ' . $id;
});
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
Note : make sure you run apache & mysql services from xamp before you serve you project.
Sorry i'm not able to tell this in the comments above as i don't have enough rep.
I think the problem is your mixing the servers you are using. First try launching your LAMP server and start apache and mysql. Since you've said that the apache launched by LAMP is pointed at port 80, you can try and access it directly on the browser (e.g. http://localhost/yourproject/public/user/1).
I'm guessing since you're using LAMP, your project is under the www root. That's why i've entered the full path for accessing the user route.

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.

Laravel remote server error 500, site doesn't works

It's my first upload to a remote server of a Laravel site.
Local I have configured a vhost so my access to my site is like:
site.domain.com //it's pointing htdocs/laravel-proyect/public
I have uploaded my site to my remote server, then:
Change permisions on storage and all its directories
Change permisions to bootstrap
Change configuration of app.php
'url' => 'http://site.domain.com',
Change configuration in database.app with new parameters (as well as in email.php)
Load all tables and data in the data base
Then I try to load my site and get a 500 Internal Server Error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
It works a little, I have this code in routes.php:
// Begins in the login view
Route::get('/', function()
{
if (!Sentry::check()) {
$data = array();
if (Session::has('email')) {
$data = array('email' => Session::get('email'));
}
return Redirect::route('login', $data);
}
else
return Redirect::route('users');
});
/ ========================================
// LOGIN, LOGOUT AND ACTIVATION SECTION ==========
// // ============================================
// show the login page
Route::get(MyHelpers::textLang('login','routes'), array('as' => 'login', function()
{
if (Sentry::check())return Redirect::route('users');
else {
$rules = User::$rules_login;
// show the login page (app/views/frontend/login.blade.php)
return View::make('frontend.login', compact('rules'));
}
So at first time mo url look like:
site.domain.com/entrar
'entrar', (login in spanish), y set by MyHelpers::textLang('login','routes'), access to my class MyHelpers and to lang files to translate 'login' in to 'entrar', but dont load the template.
Begins to :
Read documentation, making some changes: deleting Multiviews from
.htaccess (deprecated), adding RewriteBase to .htaccess too.
Copy /public content to base dir and change paths in bootstrap/paths and
in the index.php files.
Reinstall in two remote servers to verify is not my provider failing (same error). Talking to my provider, supouse
there are no errors on the server.
I create a new htaccess in the base dir redirecting routes to /public.
Try with php 5.4, 5.5 and 5.6
Actually my .htaccess is:
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]
And I have no more ideas and cant find other solutions as related here.
Any idea? Please, I'm getting crazy, on wensday I have to beguin a new proyect and still working with this one.
Thanks
I have to add
RewriteBase /
to the .htaccess
I have a demo laravel application on heroku Simple Blog.My .htaccess file is:
<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>
#other lines for gzip
It seems that there is no issue with your .htaccess file. It may be problem with you code logic. If you are getting no error with same code on localhost, then check your config again. For laravel 4, there is already a similar issue on stackoverflow Laravel 4 Virtual Host and mod rewrite setup
Note that laravel 5 (if you are using it) takes input (database connection and other setting) from .env file.
If you are still getting error then try to send simple response on index page like:
Route::get('/','basic configurations are correct');
and then make it complex step by step. It will help you in finding error.
Answer from #kikerrobles really works.
your final .htaccess file should look like following
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
# 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>

Laravel Route::controller() won't work on different URI's (PHP version 5.5, Laravel version 4.2.*)

Goodevening all programmers!
A few days ago, I buyed a domain + hosting. I had made a local project with Laravel 4.2.x, and I wanted to switch it to my online-server. I noticed that I needed PHP version 5.5 for running this version of Laravel, so I changed this in my .htaccess file.
All seems to be correct now, but it isn't correct jet. De links (href) to other pages won't work! Every time I routed to a URL without the '/' URI, I got an error page wich says that the server can't find this document.
My files:
// routes.php
<?php
Route::controller('/', 'PageController');
//Route::get('/', 'PageController#getIndex');
//Route::get('/contact', 'PageController#getContact');
PageController.php
<?php
class PageController extends BaseController {
protected $layout = 'master.master';
public function getIndex() {
return View::make('pages.index');
}
public function getContact() {
return View::make('pages.contact');
}
}
And my views: Everything is correct because it is possible to access the contact page at the '/' URI, but I just can't access pages at other URI's.
Thanks in advance!
Your code is fine, but routing is case sensitive. So http://example.com/Contact is not the same as http://example.com/contact
The first one (upper case C) should fail with a "Controller method not found" error. The second one (lowercase c) should work.
If it is not a case sensitivity issue, then your .htaccess file is not correct, missing, or unsupported on your web host. Here is an example of a know good .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>

Categories