How to redirect to

Hello all,

I’m facing serious problems to figure out how to redirect a subdomain to my primary domain. Specifically I want al requests under to redirect to On my Ubuntu 14.04 I have created a Vhost and inside the document root I have added a .htaccess file with the following directive:

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^ [NC]
  RewriteRule ^(.*)$$1 [R=301,L]

I also tried the above directive directly in apache2.conf file but with no result. When I try to go to the url I get the Apache2 Ubuntu Default Page

In my DNS records I have A — mydomain — droplet IP A — @ — droplet IP A — * — droplet IP CNAME — www — mydomain.

I Probably don’t need the * A record but after so many suggestions and attempts I’ve lost track of what should and shouldn’t be there.

I would like to set it up directly on Apache instead of using .htaccess but any suggestion/help would be highly appreciated.


@proko Clear everything in the second virtual host and try this:

<VirtualHost *:80>
    Redirect "/" ""

Can you paste the vhost file for the domain Is this vhost enabled? Check in /etc/apache2/sites-enabled/

Thanks @jesin. Works like a charm! It can also be used for post to post redirects since also redirects to In my case the slugs are different but it’s good to know. Many thanks again.

Hell @jesin, I am not completely sure for the correct syntax but just adding one line with ServerAlias inside the existing <Virtualhost *:80> tag did not work. However creating a second <Virtualhost *:80> tag within the site.conf file with the new ServerAlias did the job. Well, did half the job. I now get redirected to instead of which was the original intention. This is a great improvement of course since google clicks now at least reach the site but do you know if there is a way to go the extra step with this technic?

Hi @proko,

You do not have to create a separate vhost, edit the existing vhost for and add as a ServerAlias.

Hi jesin, Many thanks for your comment. That’s probably the piece that I miss. There is a vhost for the main domain but not for the subdomain. The subdomain was being served by another server until now. I will create the vhost asap and report back.

Yes positive, Mod rewrite is already enabled. Meanwhile I’m giving a try to Cloudflare as a workaround. Still pending status waiting for nameservers to change.

Are you sure mod_rewrite is enabled?

sudo a2enmod rewrite
sudo service apache2 reload

Hey. This article may help you setup your redirections easily!

Also if you are on CloudFlare, then use Page Rules to redirect traffic from sub-domain to your folder with a permanent rule.

Hi saurabn and thanks a lot for your comment. Actually the problem is not the linking. The site launched in a subdomain as a blog only and later on the contents of blog.domain were moved to domain/blog as part of the corporate site. The internal links already point to the right place. Problem is that there are still older posts appearing in google search with the old urls. So the idea is that redirects to need for post to post mapping).

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This question was answered by @jesin:

@proko Clear everything in the second virtual host and try this:

<VirtualHost *:80>
   Redirect "/" ""

View the original comment