2

I'm creating a blogging platform where each user got their own subdomain for their website. So I was thinking is it possible using .htaccess to redirect the user to the directory they entered in the URL.

I'm basically looking for a solution to dynamicaly create a new blog using the platform and when it does that it'll create a new laravel project and that project will have its own subdomain.

E.g.

Blog1.example.com will show the content from /blog1/public

Blog2.example.com and will show the content from /blog2/public

Is that possible or should I look for a different solution?

Im using Apache2 and laravel

Matilde
  • 31
  • 1

1 Answers1

0

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.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • What I'm thinking is actually that every time someone creates a new blog I'd run a command that creates a new Laravel application instead of running it all in one Laravel application I'd like to run each blog with's it's own Laravel app using the blog.example.com, blog2.example.com, e.t.c. – Matilde Apr 13 '20 at 11:05
  • No need for that. What would be the reason for that kind of approach? – Tpojka Apr 13 '20 at 11:18
  • Later on, I would allow users to install their own plugins by building a plugin manager. I'm just thinking if I had to run it on one application running a lot of plugins would be overkill and would be better to separate it into their own app so it doesn't contain too many plugins. – Matilde Apr 13 '20 at 11:33
  • Lot of wishes without single line of code. You are raising the bar with every new comment. I think nobody on Stackoverflow can help until you have concrete question that explicitly include code of where error happened. Warmly advice to read this article of [how to ask perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Or maybe you would like to ask [here](https://softwareengineering.stackexchange.com/) (Software Engineering) until you don't have concept and concrete code question for SO community. – Tpojka Apr 13 '20 at 11:41