-->

How do I enable custom domains for my users?

2020-08-23 17:47发布

问题:

I am looking to allow my users to use their own domain name to access my web service.

For example if I have a user with a profile at example.com/users/david how can I allow david to use exampledavid.com to access his profile page.

I don't want the users to see example.com in the address bar, but instead for david to see exampledavid.com.

回答1:

Your issue can be solved with URL rewrite and HTTP Header manipulation or reverse-proxy.

  • For apache http server: use ProxyPassReverse directive

The directive ProxyPassReverse lets Apache adjust the URL in the Location header on HTTP redirect responses. For instance this is essential when Apache is used as a reverse proxy to avoid by-passing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

Suppose the local server has address http://wibble.org/; then

ProxyPass /mirror/foo/ http://foo.com/
ProxyPassReverse /mirror/foo/ http://foo.com/

will not only cause a local request for the http://wibble.org/mirror/foo/bar to be internally converted into a proxy request to http://foo.com/bar (the functionality ProxyPass provides here). It also takes care of redirects the server foo.com sends: when http://foo.com/bar is redirected by him to http://foo.com/quux Apache adjusts this to http://wibble.org/mirror/foo/quux before forwarding the HTTP redirect response to the client.

  • For MS(R) IIS use Re-Write Module:

Easily replace Web application URLs to produce user and search engine >friendly results. URL Rewrite permits Web administrators to easily replace the URLs >generated by a Web application in the response HTML with a more user friendly and search engine friendly equivalent. Links can be modified in the HTML markup generated by a Web application behind a reverse proxy. URL Rewrite makes things easier for outbound response content and headers rewriting with outbound rewrite rules that work with HTTP request and response headers and with IIS server variables.

Additionaly, you must make sure that exampledavid(dot)com is setup with DNS provider to pass all requests to example.com.

DNS Record Sample:

NAME                    TYPE   VALUE
--------------------------------------------------
exampleXYZ.com.         CNAME  example.com.
example.com.            A      192.0.2.23

Ref:

  1. https://en.wikipedia.org/wiki/CNAME_record

  2. http://www.akadia.com/services/apache_redirect.html

  3. http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

  4. http://www.iis.net/downloads/microsoft/url-rewrite



回答2:

You can easily enable this by telling your clients to configure a CNAME to point to your domain.

So if your server is located at www.example.com you tell 'david' to configure www.exampledavid.com to have a CNAME record pointing to www.example.com

At the server end you would have a configuration that detected the domain that was being requested and redirected and served the appropriate content to 'david'

If your clients want to use the naked domain i.e. exampledavid.com to your servers, you would need to provide them with an IP address, however before doing this you would need to be sure that your IP address wasn't going to change, and probably have a contract with whoever supplies it to ensure that.