Laravel shows 'index of' page only on certain routes - php

I've noticed some people have had similar issues to this, but the issue has been to do with their vhosts and document roots, but most of the URLS on my site seem to be working fine.
For example, if I create the following routes
Route::get('cases', function(){
dd('here');
});
Route::get('bookings', function(){
dd('there');
});
my.local/bookings works absolutely fine, but my.local/cases just shows me the index of /cases page.
I have rolled back my code to a time it was definitely working, but its still returning the index of /cases page.
Why would laravel randomly stop some routes working?
Heres what i've looked at so far
Rolling my code back to a working state
Dumping composer autoload
Checking php artisan route:list to make sure it exists
Trying a server alias to see if a local IP would work instead
Changing my PHP version
It just wont show me this one page?
Any ideas?! Its driving me mad!
Note - /cases/create works fine, my index / page works fine, it just seems to be a GET request to /cases

The reason for this behavior is the default .htaccess rewrite rule:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The two conditions define that the request should only be forwarded to index.php (and therefore Laravel) if there isn't an actual file or folder matching the URL.
It seems like there is a cases folder inside public, so Laravel will not handle requests to that folder.
You can either rename the public/cases folder / the route or remove the first RewriteCond condition. But removing it might have unintended side-effects.

Related

PHP friendly URL bug?

I just started a course of implementing MVC pattern using PHP and I think I'm experiencing a bug regarding friendly URL engine. The site I'm creating has the follow URL syntax: http://[site]/[controller]/[method]/[parameters] .
Until now it works in a very simple way: I access a controller and is printed on screen which controller is beeing accessed (printed by a method from controller's class). The problem is that when I access the URL http://cursophp/cliente (a real controller) it works and when I try an inexistent controller WAMP shows an error screen that informs that the page couldn't be found; but, when I try to access http://cursophp/produto (also a real controller), an 404 error from Apache is exhibited! And I also tried with other words like: produto1, produto2, product, product1 ("prod" prefix) and the same 404 error is exhibited!
Using Composer or including directly the controller file with "require" results in the same problem.
Is it a known bug or am I making a mistake?
htacess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !f
RewriteCond %{REQUEST_FILENAME} !d
RewriteRule ^(.*)$ index.php/$1 [L]
Just adding more details:
When I try "pro" as controller's name:
When I try "prod" (or any word with "prod" prefix):
When I try "aaaa" (or any word, of any length, that doesn't correspond to a controller's name):
The problem is probably in function that checking the route. Try to debug on which line it's starting show the error. However, it's impossible to suggest you a specific way of solving this problem without providing more details
This PHP script is crappy, because it does not handle unknown controllers properly.
Instead it should respond with HTTP404, when the file to load cannot be located.
Generally speaking, you'd need to fix that in file Core.php.

Can Symfony 4 be configured to ignore code installed in subdirectories?

I'm currently converting an old website to use Symfony 4 and the site uses the LiveZilla live chat app.
LiveZilla lives in a subfolder of the site, and is accessed directly using URLs under somesite.com/livezilla. I can't currently access it of course, because I haven't configured a route for this folder. So Symfony decides that this will be a 404.
LiveZilla is essentially a completely separate app living in its own folder here. It isn't dependent on the website hosting it. Is there a way to tell Symfony to ignore certain paths so that code like this can be executed without interference?
I have a sneaking feeling that I need to adjust the way I am looking at this as I can't find anything obvious in the Symfony docs about this, and the framework is pretty well thought out. The best I have come up with so far is hacking public/.htaccess, but it feels wrong somehow...
Your .htaccess file should allow requests directly to existing files, but not directories. See this rule:
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
This means you should be able to access somesite.com/livezilla/index.php but a request to somesite.com/livezilla will redirect to the symfony front controller. So try changing your links to point to actual files within the sub-directory.
There is also nothing wrong with editing the .htaccess file to suit your needs. You just need a condition that checks if the request is to the sub-directory and if so use the same RewriteRule ^ - [L] as above to allow that request to continue.
The following should work if placed after the above rule (reference):
RewriteCond %{REQUEST_URI} ^/livezilla/
RewriteRule ^ - [L]
Or this may be better, place this rule immediately after the line RewriteEngine On(reference)
RewriteRule ^(livezilla) - [L]
The [L] flag means the rule will be the last one used for the request.

Admin folder not working after adding php router (AltoRouter)

I am working on a php project. I am using AltoRouter for this one. In order for AltoRouter to work I need to add this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
to the .htaccess file. What this does is, correct me if wrong, make sure that all request (hyperlink) is first carried to the index.php. No problem till now and everything works great.
I have an admin folder and to login I have to go to
http://www.example.com/admin/
but it shows nothing. I believe the newly added AltoRouter is the cause for this problem. Can any other AltoRouter / php-router user out there help me figure this out.
P.S. Not sure but do I need to Map the Admin Folder as well?

Laravel is "listening" to index.php string anywhere in the URL

I have a Laravel 5 project with routes set up as well as a custom 404 page (mostly for missing/incorrect "pages").
So basically if I open any existing route I get the correct output and every other URL is showing the 404:
project.com/login - Fine, login page
project.com/ghdkfgl - 404
This looks clear and seems to be working as expected. So anything I add after the slash opens either an actual existing page or a 404 page.
Unless I put a 'index.php' anywhere in the URL. In this case, Laravel is executing the request for some reason like this:
project.com/jhdfkds/index.php/login - Opens the login page (the CSS and other resources are gone because of the paths but that's clear).
project.com/kfhjdsg/index.php/fkdhsg - Opens a 404 (but the CSS and other resources are not loaded too).
I'm sure both of these should open the 404 since there's no such routes in my project.
I also checked for the same behavior on the Laravel documentation website (I assume its built on Laravel).
http://laravel.com/docs/5.0 - Actual URL
http://laravel.com/aaa - A nice 404 page
http://laravel.com/aaa/index.php/docs/5.0 - Laravel documentation page again, same as the first one
What might be causing this? How can this be solved?
Why would Laravel even consider the 'index.php' in the middle of the URL?
Does this have anything to do with the .htaccess file? (I didn't edit it though)
The problem is inside the Symfony Request class and how it determines the base URL. Basically it assumes that if you have a request like aaa/index.php that your currently running script (named index.php) is inside the directory aaa takes aaa/index.php as base URL.
Later it will then be stripped from the actual request URI.
I fixed this unwanted behavior with a pull request that is currently under review. I will update this post as soon as it gets merged.
Same issue i was facing in laravel 3 when i was skipping public from url. then i have placed another htaccess file in public folder.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

How to understand PHP's URL parsing/routing?

I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.

Categories