php.htaccessdnslampregistrar

Third party url pointing to a certain directory on my website


I have a website with some user profiles on it.

Example:

-mysite.com/profiles/user-a
-mysite.com/profiles/user-b

Each profile can have additional features such as articles, a contact page and so on.

Examples:

-mysite.com/profiles/user-a/articles
-mysite.com/profiles/user-a/contact

I want to allow my users to register a domain and use their profile (the one they created on my site) as their own website but with their own domain.

Examples:

user-a.com
user-a.com/articles
user-a.com/contact

This way they'll be able to update and have a profile on my site, but at the same time have their own website!

Is there a technical way to do so? What is the easiest one that will allow the least possible amount of work for my users?

Thanks, Luca


Solution

  • Firstly, you need to configure your web servers to serve your site on any domain. In Apache, this is done by modifying your VirtualHost configuration, like so:

    <VirtualHost *:80>
    

    Also, make sure that this config is loaded first. (For more details see: Apache default VirtualHost)

    Next, you'll have your users set up CNAME records on their DNS server that points their hostname to yours. This makes it so that their hostname resolves to the same network addresses as your hostname. Note that they cannot set a CNAME record for the top-level domain.

    You will probably also want to have a place in your site where the user can configure which hostname(s) they're using.

    Finally, you need a way to handle these paths that don't have user-a in them. There are two ways to do this. The first is to modify your rewrite rules to handle these paths directly, and use the Host request header in your application to figure out how to handle it.

    The second way is to actually use your rewrite rules to inject the Host header value into the URL, which then you can read in your application. Here's an example from the Apache documentation:

    RewriteRule  "^/(.*)$"         "/www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1"
    

    In the end, all your users do is update a CNAME record, and update the config on your site. Once set up on your end, this is easy to maintain.