Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Laravel - Allow users to use a custom domain name

I've looked through pretty much every single page I could find on the topic, but I'm still bit confused.

I have a PHP Laravel 4.0 web site, which is at

http://www.mywebsite.com

When a user signs up, each user gets their own page

http://user1.mywebsite.com

I have set the above via wildcard sub domain on my VPS DNS manager (Linode).

And in my Laravel, I have a domain route that picks up the subdomain and displayes the correct user's page. All works well.

Now, what I want to achieve is have a user use their own domain name and point to their page on my website.

So if a user signs up with

http://www.user1domain.com

I'd like this domain to point to http://user1.mywebsite.com

I do not want a redirect, but the URL to remain at http://www.user1domain.com (obviously).

I know that sites like Tumblr allows this.

What steps do I take in achieving this? Here are somethings I thought of during my research.

  1. Have the user to make their custom domain "user1domain.com" to point to my VPS name server (easy).

  2. On my end (here's where I fall short), do I

    • Create a new DNS entry? (In my Linode VPS, I would do this in "DNS Manager" and "Add a domain zone")

    • I'm using Apache, so do I have to create a VirtualHost (like I do for all of my other sites), does this mean I have to create a new VirtualHost file for every user who wants to use a custom domain?

    • If NOT VirtualHost, how do I actually point to my web application folder? Apache alias? (How do I set that up?)

  3. Assuming the user's domain name now "points" to my web application, how do I now pick that up in Laravel? (I'm guessing I'd do this the same way I do for my sub domains)

Thanks for your time.

like image 958
ericbae Avatar asked Jun 20 '14 12:06

ericbae


1 Answers

Question: Create a new DNS entry? (In my Linode VPS, I would do this in "DNS Manager" and "Add a domain zone")

Answer: Yes, you'll need to ask your user to point their domain to your nameserver. You'll also have to add their domains into your DNS Manager.

Question: I'm using Apache, so do I have to create a VirtualHost (like I do for all of my other sites), does this mean I have to create a new VirtualHost file for every user who wants to use a custom domain?

If NOT VirtualHost, how do I actually point to my web application folder? Apache alias? (How do I set that up?)

Answer: You can set up just one virtual host configuration for all, as documented in Using default vhosts.

<VirtualHost _default_:80>
    DocumentRoot /var/www/mysharedsite
    # ...
</VirtualHost>

So all domain names that are requesting to your server will be served by your only one code base.

Question: Assuming the user's domain name now "points" to my web application, how do I now pick that up in Laravel? (I'm guessing I'd do this the same way I do for my sub domains)

I can think of two ways right now. Yes you could adapt Laravel's documentation on Sub-Domain Routing, but use for different domains instead:

Route::group(array('domain' => 'user1domain.com'), function()
{
    Route::get('blog/{id}', 'BlogController@view');
});

But I suppose that functionalities will be quite the same between different domains, it's only the data that changes, you could use always use:

Request::getHost();

So you get the domain, and perhaps map it to a website_id column in your database to get the correct content.

Make sure you whitelist the acceptable domain names carefully in your project. Letting all domain names in isn't a good idea. You could limit by using ServerName and ServerAlias in your virtual host as well, but I guess that limits the flexibility of letting new users have their own domains.

like image 149
Unnawut Avatar answered Sep 21 '22 02:09

Unnawut