Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matt_panton's avatar

Valet 404 on non Laravel php sites

Would really appreciate it if someone could help. I've just installed Laravel Valet and it serves Laravel sites and static html sites fine but I get a 404 with site that uses php but no framework. Is there any way to solve this?

0 likes
25 replies
vitorf7's avatar

Valet is a Laravel development environment for Mac minimalists.

This is the very first line of the introduction in the documentation for valet. Based on that I assume that it will not work for anything other than Laravel.

matt_panton's avatar

It supports Wordpress and Statamic and static html sites not just Laravel, I was just wondering why plain old php sites are getting a 404.

jackabox's avatar

Did you get this resolved? Having the same moment right now

matt_panton's avatar

@jackabox Havn't managed to resolve it yet. I'm guessing it just needs a custom driver but I'm not clever enough to create it :(

simondavies's avatar

I created a Static Site driver so that it points to the main index.php page on the root of the site, seems to work initially. See bleow for what i did they are simple really.

  class StaticSiteValetDriver extends ValetDriver
  {
    /**
     * Determine if the driver serves the request.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return void
     */
    public function serves($sitePath, $siteName, $uri)
    {
        if (file_exists($sitePath.'/')) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the incoming request is for a static file.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string|false
     */
    public function isStaticFile($sitePath, $siteName, $uri)
    {
        if (file_exists($staticFilePath = $sitePath.'/'.$uri)) {
            return $staticFilePath;
        }

        return false;
    }

    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        return $sitePath.'/index.php';
    }
  }

Seems ok but any linkable pages form here need to be in the html format as if i name them .php then you get the error:

 Notice: Undefined index: php in /Users/PremierPR/.composer/vendor/laravel/valet/drivers/ValetDriver.php on line 112

So if not running a type of frame work and just simple php pages not seeming to get these to run??

matt_panton's avatar

@simondavies I did that too and thought I had it working until I tried going to any page other than the home page.

bashy's avatar

Non-Laravel site which is PHP... but is it single PHP files or via an index.php file with URL-rewrite?

bashy's avatar

Really simple then, just direct the URI to the filename in a directory. Or just setup this shit separately without Valet :P

matt_panton's avatar

@bashy I'm probably being really dense but what do you mean by direct the URI to the filename? I'm tempted to do it without Valet but it'd just be sweet if all my projects, laravel or not, could be served on a .dev url without any messing about

bashy's avatar

Oh, sure. This should work without much changing

/**
 * Get the fully resolved path to the requested PHP file.
 *
 * @param  string  $sitePath
 * @param  string  $siteName
 * @param  string  $uri
 * @return string
 */
public function frontControllerPath($sitePath, $siteName, $uri)
{
    return $sitePath.'/'.$uri;
}
1 like
matt_panton's avatar

@bashy thanks for your help, I've got it working with this. I'm using a htaccess rewrite to remove the .php extension from the uri so had to add it on.

public function frontControllerPath($sitePath, $siteName, $uri)
    {
        return $uri == '/' ? $sitePath . '/index.php' : $sitePath. $uri . '.php';
    }
2 likes
bashy's avatar

Ah okay! And of course you'd need the index.php if it's just /, forgot about that.

simondavies's avatar

@matt_panton and @bashy Sweet! Although it took me a while to realise that i had links with .php on them, and this was causing an error,

eg

 <a href="nextpage.php">

should be(no .php, as that is accounted for in the code)

 <a href="nextpage">

So now its sorted, thanks.

matt_panton's avatar
matt_panton
OP
Best Answer
Level 12

Just for anyone that wants to copy and paste a Driver. This is my BasicPHPValetDriver.php file stored in ~/.valet/Drivers. It works with urls that either have or don't have the .php extension. :)

<?php

class BasicPHPValetDriver extends ValetDriver
{
    /**
     * Determine if the driver serves the request.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return void
     */
    public function serves($sitePath, $siteName, $uri)
    {
        if (file_exists($sitePath.'/index.php')) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the incoming request is for a static file.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string|false
     */
    public function isStaticFile($sitePath, $siteName, $uri)
    {
        if (file_exists($staticFilePath = $sitePath.'/public/'.$uri)) {
            return $staticFilePath;
        }

        return false;
    }

    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        if ($uri == '/')
            return $sitePath.'/index.php';

        return strpos($uri, '.php') ? $sitePath.$uri : $sitePath.$uri.'.php';
    }
}
2 likes
simondavies's avatar

getting an error now when using @matt_panton BasicPHPValetDriver driver where as it will not display a page within a folder

domain.dev/test/hello.php

This just loads the homepage, but if i leave the filename off domain.dev/test all it will do is download file, then when i open the file i get:

       Notice:  Undefined index: extension in /Users/PremierPR/.composer/vendor/laravel/valet/cli/drivers/ValetDriver.php on line 121

Any ideas how to fix this or for another driver to sort these sub folder pages etc

bashy's avatar

@simondavies Probably need something similar by just using the URI as the path. See my reply on first page.

simondavies's avatar

@bashy thanks but nothing what been done on this page seems to work. The URL will change to the request but no matter whats been asked for it will always return/display the main index.php and no other static .php files etc, even if i have a help.html page , thanks using v1.1.12 of valet

bashy's avatar

@simondavies So if you return and include test/hello.php via the method, you still get the index page or the test/hello.php one?

simondavies's avatar

@bashy so if i add the following?

    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        return $sitePath.'/test/hello.php';
        // if ($uri == '/')
        //     return $sitePath.'/index.php';
        // 
        // return strpos($uri, '.php') ? $sitePath.$uri : $sitePath.$uri.'.php';
    }

then in the browser just got to domain.devthen i still get the main index.php.

my folder structure is that the public facing files are with in a public, whilst the rest are on the root. for more info.

simondavies's avatar

the error seems to relate to the ValetDriver.php file on this part of code, regarding $extension = pathinfo($staticFilePath)['extension'];

    public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
    {
        $extension = pathinfo($staticFilePath)['extension'];

        $mimes = require(__DIR__.'/../mimes.php');

        $mime = isset($mimes[$extension]) ? $mimes[$extension] : 'application/octet-stream';

        header('Content-Type: '. $mime);

        readfile($staticFilePath);
    }

But this only when i visit the folder with no file name EG: domain.dev/test/ then a text file downloaded and thats the error i got earlier.

If i change that to :

   $extension = pathinfo($staticFilePath, PATHINFO_EXTENSION);

then i still get the same but the file is empty this time no error message. But this still does not solve the issue that i'm currently getting though, just thought i would add more details....

simondavies's avatar

@bashy I copied the site over to out local php server, and then run the site form there to pick any issues that i might have done with the normal site(ish) but it seems to run OK as i would expect so looks like a valet thing, might try my local homestead tomorrow as well.

Thanks for the advise so far.

araujophillips's avatar

It doesn't work for "plain PHP" sites which tipically uses "/public/" as root path to handle pages (My case).

Adding the following fixes to the frontControllerPath method, it works.

/**
 * Get the fully resolved path to the application's front controller.
 *
 * @param  string  $sitePath
 * @param  string  $siteName
 * @param  string  $uri
 * @return string
 */
public function frontControllerPath($sitePath, $siteName, $uri)
{
    if ($uri == '/')
        return $sitePath.'/public/index.php';

    return $sitePath.'/public'.$uri;
}

Please or to participate in this conversation.