It's possible.
First you have to allow/enable wildcard subdomains at your domain registrar/provider's dashboard.
Then you set your application routes to support subdomain routing.
https://laravel.com/docs/7.x/routing#route-group-subdomain-routing
Logic is that you limit no requests to
anything.example.com
since you've enabled
*.example.com
at registrar's. If anything
doesn't exist in you blogs.slug
column you would
Then you make that sort of filter through laravel's subdomain routing shown in docs at that link above.
Route::domain('{slug}.example.com')->group(function () {
Route::get('blog/{slug}', function ($slug) {
// pseudo code
//
// if $slug doesn't exist
// redirect to some generic or 404 page
});
// or
// Route::get('/blog/{slug}', 'BlogController@show')->name('blog.show');
});
In controller you'd have something like
BlogController extends Controller
{
public function show(string $slug)
{
$blog = Blog::where(['slug' => $slug])->first();
return view('blog.show', compact('slug'));
}
}
// this block is implemented valid way if you use routing call that is commented out `Route::get('/blog/{slug}', 'BlogController@show');` instead anonymous callback function shown before that line in route file.
There are other things you can implement such as setting primaryKey in Blog model to be 'slug'
field then you would be able to use implicit model binding with public function show (Blog $blog) { /** return $blog */ }
etc. but this is something where you can start. Basically no need for .htaccess
changes.