Why do I get a 404 to laravel 5.7 public subfolders? - laravel

I have a Laravel 5.7 project and I can't seem to understand why I get a 404 error to the subfolders in the public directory.
Directory structure
public
icons
css
file.css
Example of routes that work just fine:
localhost/images/image.png
localhost/css/main.css
However when i try to request a file deeper that the first subdirectory, i get a 404. For exaple:
localhost/pugins/common/common.min.js
file located in public/pugins/common/common.min.js
localhost/icons/weather-icons/css/weather-icons.min.css
file located in public/icons/weather-icons/css/weather-icons.min.css
Anything after localhost/FOLDER/ gets a 404 eror, even if it exists.
Any ides why?
This is the default .htaccess 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 have noticed just now that the first level of the subdirectories of the public folder get a 403 (Forbidden) error while try to list that directory.
Example:
localhost/js => 403, Forbidden
localhost/js/dashboard/intro-inde.js => 404, Not found
localhost/js/script.js => 200, Working fine
Each file and directory exists in the examples above, and there is no problem with the permissions.
My guess is that it has something to do with the .htaccess, but i have no idea how that works >.>

I remembered i had no problems in previous projects using laravel 5.1 ~ 5.5. So I have copied the .htaccess file from those projects to this one. Now everything works fine, but for one folder and its subfolders : public/icons; I solved this problem by creating a new folder and moving everything from "icons" to "fonts-icons"
Behold the power of the new .htaccess. Maybe it will help someone.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
This one solved the problem...without the new for new routes. :D I still have no idea how it works and why, though.
Just for curiosity, maybe someone will enlighten me d=(^_^)=b

You should first define that route in the routes/web.php file.

Error 404 is when we do nnot find any pages. Which means we have to define route in our web.php file first so as to view desired content written in the blade.php file, This error may also appear if we have not saved the file in our views in .blade.php extension

Related

Is there any changes required in the .htaccess file to acces the API routes?

I have added a few routes to the api.php file. These routes are working perfectly in the localhost environment.
http://localhost/api/v1/events
gives desired output but when uploaded to the production/remote server I cannot access it using the domain name.
http://domainname/api/v1/events
throws an 404 error. I am using the same .htaccess files in the localhost as well as in the prod/remote server. I am not sure why is it not working correctly in the prod/remote server.
By the way, I have web routes which works fine in both the environments. My .htaccess file is copied below.
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Modified .htaccess as per the answer but it is not working for me :(
<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}]
# API
RewriteRule ^api/(.*) api.php/$1 [L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Tried this as well.
RewriteRule ^api/(.*) routes/api.php/$1 [L]
Can anyone help by advising what is wrong?
My root folder
the folder in which api.php is available
The confusing part about this is that you say it works "perfectly in the localhost", yet you are using the same .htaccess file in both environments. This cannot work on localhost unless you have some other directives somewhere since there is nothing that would rewrite such a request to /api.php.
To enable this, and pass the additional path information as path-info to api.php, then you would need to add something like the following after the # Handle Authorization Header rule:
# API
RewriteRule ^api/(.*) api.php/$1 [L]
This would internally rewrite a request of the form /api/v1/events to /api.php/v1/events, allowing api.php to read /v1/events from the $_SERVER['PATH_INFO'] superglobal.
Without this directive (and with MultiViews being disabled) a request of the form /api/v1/events would be routed through index.php instead where I assume the "route" is not defined, hence the 404.
UPDATE#1: No my /api.php is under the same directory which has web.php, that is routes
In that case, you need to modify the above rule to read:
RewriteRule ^api/(.*) routes/api.php/$1 [L]
UPDATE#2: from your updated URL structure, it seems that /routes is actually under /myprojectfolder in the document root (where the .htaccess and index.php files are located), so you would need to modify the above to account for this. For example:
RewriteRule ^api/(.*) myprojectfolder/routes/api.php/$1 [L]
However, it seems that a direct request for /myprojectfolder/routes/api.php/v1/events does not work anyway, so passing this as path-info would not seem to be the correct course of action to begin with.

all links is not working except home url in Laravel

I have successfully run a Laravel project in Xampp. But only home url is working perfectly. But for all other links(ex : menu, item details etc) css and js is not working.
my home url is
http://localhost:90/office/sencare/
and sample css or js links are
http://localhost:90/office/sencare/frontend/css/style.css
But for all others links this css or js links will change automatically ex :
http://localhost:90/frontend/css/style.css
office/sencare is missing after localhost:90.
.env file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Dm34MLg8AbQk4ADyIG9cYPaIwYbQgrUgrN7Ani/x+JA=
APP_DEBUG=true
APP_URL=http://localhost:90/office/sencare/
.htaccess 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>
sample.blade.php
Can't find the solution where to change.
Anybody help please ? Thanks in advance.
Please tell me if anything else needed
rootfolder/public/frontend/css/style.css
this should be the path to style.css as per your code
verify it

Laravel 5.8 showing 404 Not Found errors

I change the index.php file path public to root. After adding a .htaccess file in the root path (laravel 5.7) every page working fine. But in laravel 5.8 when I click another page, it's showing 404 Not Found.
My .htaccess file is below.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d``
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The content of a Laravel .htaccess file should look like this:
<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>
Your webroot has to be set to the /public directory. Don't try to search for other solutions that move the index.php and .htaccess file from /public to the root directory of your Laravel application. There is absolutely no reason why you want to move the webroot from /public to /. index.php sits in /public for a reason. If your webhost does not offer the ability to move the web root to another directory switch your hoster.
It might be that you have created your folder as Example and trying to access it via example
ie
localhost/Example/public
yet accessing it like
localhost/example/public

Laravel showing directory file instead of redirecting to public/index.php

I am kinda new to Laravel and I am trying to do the next thing:
I have a Centos 7 running Apache and PHP on which I have different web apps running.
I have all apps in /var/www/html under folders and index.php links them all. Now i installed composer, Laravel and added a new Laravel project in /var/www/html/play/play1.
The problem is that when I try to access https://example.com/play/play1 I should get the Laravel index page (the one under public/index.php) correct?
Instead I get a directory with all Laravel project files.
If I access https://example.com/play/play1/public I can see the index page.
I've searched it over and I checked if AllowOverwrite all is present, if mod_rewrite is on. Inside public folder there is a .htaccess file.
Can anybody who dealt which such a problem help me please ?
Within the root of the project folder (where you see app/ resources folders etc) create a .htaccess file and paste the below:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Laravel's root directory should not be the one accessed publicly - you have to point to public/ directory (where the index.php and .htaccess are located).
So in your server config http://example.com/play/play1 should point to var/www/html/play/play1/public

Laravel routes are not working on live server

I just found the problem on server. all is working fine in my localhost, but on live server, ONLY the home page route is working.
My directory is:
laravel-
css
js
local->
app
HTTP->
Controllers->
Homecontroller
admin->
Groupcontroller
config
...
Here is my htacess
<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]
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my route file is:-
Route::get('/group/detail', 'Groupcontroller#index');
Route::get('/group/add', 'Groupcontroller#create');
Route::get('/group/edit/{id}', 'Groupcontroller#edit');
http://www.example.com/home
My home controller is working.I think issue is with admin folder???
http://www.example.com/admin/group/detail
This is not working
Error is encountered :-
Class App\Http\Controllers\Admin\Groupcontroller does not exist
Please help me,Working fine at localhost but not on live.
Thanks in advance
Try and change the content of your .htacess to this
<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>
Might be you forget to paste .htaccess file from /public folder to /public_html folder
so Just COPY And PASTE the .htaccess file from public folder to /public_html folder
then it will work as local
Path of Groupcontroller is Controllers/admin/Groupcontroller. So in your routes you need to access Groupcontroller with appropriate path.
Route::get('/group/detail', 'admin\Groupcontroller#index');
Route::get('/group/add', 'admin\Groupcontroller#create');
Route::get('/group/edit/{id}', 'admin\Groupcontroller#edit');
Also it is recommended to use CamelCase folder names. that is; change admin => Admin.
Check the namespaces compared to directory names. The cases should match.
You can try the bellow code :
After the domain name add "public" keyword between your API routes
for example:-
https://example.com/public/api/someroute
After this, you may have to update your PHP version .

Resources