Possible to use .htaccess files using Valet?

Published 2 years ago by assertcarl

I'm following a tutorial in building something using MVC but I have got stuck at the .htaccess file, my question is can I use Valet following this tutorial or will I have to switch to Vagrant?

spekkionu

Valet does not use apache so it wont use your .htaccess files.

If you are just using your .htacess file to rewrite urls you likely can create a custom valet driver to make it do what you want.

assertcarl

Thank you for the reply, I do want it just for rewriting the pretty urls but unfortunately I wouldn't know where to start to create my own driver.

All I am looking to do is take anything after the forward slash and pass it to index.php.

Would that be difficult to implement?

spekkionu

Take a look at the official documentation I linked to.

It essentially is just writing a class that implements three methods, none of which do anything complicated.

bashy
bashy
2 years ago (1,000,470 XP)

Basically, make a new file (or include them directly in the server block) and use this syntax;

rewrite /blog/1 /blog/post/php/how-to-do-nginx-redirects permanent;
markpng

I've been working on a custom application and created the following Valet Driver to simulate the use of .htaccess files: https://medium.com/@theghostcoder/custom-laravel-valet-driver-61ede526861e

EmilMoe

I thought Valet was created to make it simple, but people have to write custom drivers? I am confused about the purpose of Valet.

Roni
Roni
1 year ago (60,800 XP)

I was running into this pretty hard the last couple of days. I've been in Laravel for the last several months, and when I came back out to the real world to transition a larger group of integrated CRM projects that was no longer able to grow easily into a laravel ecosystem it was actually quite painful.

Now I'm not a linux network guy, but after reading all these posts that say make something custom, I still couldn't figure out a good way, so here is whats working for me.

Looking through valet you can make a custom driver. Here is one of the ways you can load it, and it seems faster for me.

https://github.com/laravel/valet/blob/42834e22474c87f86047ea1fe4ac4bc3a8f16121/cli/drivers/ValetDriver.php#L88

Add your custom valet driver right in your project directory instead of ~./valet/Drivers/ This will let you leave your regular valet installation untouched.

Now, most of the reasons I'm seeing htaccess has something to do with 301's, moving things to https, and url rewriting between other technologies. I'll only deal with the rewrites because you don't need the rest on your dev machine.

Now in my case I typically start my transition by changing everything to php. I just find it helpful to do things incrementally and test. But you can't access http://your-local-proj.dev/a-random-page and have it serve the php page.

So here is my local driver to do that, hope this helps you.


<?php

class LocalValetDriver 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($sitePath . $uri) &&
            !is_dir($sitePath . $uri) &&
            pathinfo($sitePath . $uri)['extension'] != 'php') {
                return $sitePath . $uri;

        }

        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 (is_dir($sitePath . $uri)) {
            return $sitePath . $uri . '/index.php';
        }

        if ($uri == '/') {
            return $sitePath . '/index.php';
        }

        if(file_exists($sitePath . $uri . '.php')){
            
            return $sitePath . $uri . '.php';

        }

        return $sitePath . $uri;
    }
}

marcgaumont

Thanks for this @Roni life saver !

Roni
Roni
1 month ago (60,800 XP)

@marcgaumont Glad it helped. Nothing is more irritating than not being able to start in on something.

Please sign in or create an account to participate in this conversation.