This post was originally published at rohitlakhotia.com
This tutorial assumes that you have a self-hosted ghost installation up and running.
Ghost only server content on one domain at a time, so in order to serve content from other domains you need to redirect (301 vs 302) that domain to the domain where the ghost CMS is hosted on.
You may wish to have multiple domains that redirect to your site, e.g. to have an extra TLD or to support www domains. This is intentional for SEO purposes, however, you can always redirect extra domains to your Ghost install using Nginx.
If you want to redirect an HTTPS domain, you must have a certificate for it.
Basic Setup
What we will try to achieve here is to have www.rohitlakhotia.com
point to the root domain rohitlakhotia.com
with an SSL certificate so the browser doesn't show warnings while visiting any of the above.
This would also include the HTTP version to redirect to the HTTPS version so the search engine doesn't consider them duplicate pages.
Configure your secondary domain
We would need to configure the domain DNS records in order to let the browser know where to go while requests are being made.
Configure your domains with proper records
If you look closely the www
version and the @
(root) version both point to the same IP (server).
Basically, every request that points to those two versions would be handled by the server at that IP address. Which is exactly what we want. So we can handle them appropriately.
Now we would need to SSH into the server to install the certificate and properly redirect the domain.
After you have access to the server, go to the directory where you have installed ghost. Mostly it would be
/var/www/<your ghost installation>
So cd into the directory and then
ghost config url https://www.rohitlakhotia.com
In place of www.rohitlakhotia.com
, add your domain (secondary domain) that you want to redirect to the root domain.
Before proceeding with the next command just make sure that the A records are successfully updated, else the SSL certificate won't be installed.
ghost setup nginx ssl
This will generate SSL certificates for your domain (which for me is www.rohitlakhotia.com
) that you just added and also the Nginx configuration file for the HTTP and HTTPS version. (Easy!)
Now we will again change the ghost installation URL back to the old one which for me is https://rohitlakhotia.com
ghost config url https://rohitlakhotia.com
Now we would need to update the newly created Nginx configuration file so that we can redirect the user to the root domain.
The Nginx configuration files are mostly located at /etc/nginx/sites-available
cd /etc/nginx/sites-available
Here you will find all the Nginx configuration files. There should be a minimum of 4 files. Every domain has files with -ssl.conf (For HTTPS) and .conf (For HTTP).
So we would have 4 files:
rohitlakhotia.com-ssl.conf
rohitlakhotia.com.conf
www.rohitlakhotia.com-ssl.conf
www.rohitlakhotia.com.conf
Now we need to edit the files for the secondary domain.
So the files that I need to edit would be www.rohitlakhotia.com-ssl.conf
and www.rohitlakhotia.com.conf
We need to add this line of code inside the location block but at the end.
return 301 https://rohitlakhotia.com$request_uri;
You can change the 301 to 302 depending on your use case (301 vs 302). You will replace rohitlakhotia.com
with your primary domain.
What this line would do is whenever a request is made to the secondary domain then it would redirect (301/302) it to the specific URL.
So both the files should look like this
Now check if the configuration files are valid by
sudo nginx -t
If it's successful then reload the Nginx server
sudo nginx -s reload
And that's it.
You just pointed your secondary domain to your primary domain in ghost.
For more content you can checkout my blog
Top comments (0)