Question

How to redirect blog.domain.com to domain.com/blog

Posted July 26, 2015 8.5k views
UbuntuApacheLAMP Stack

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 blog.mydomain.com to redirect to mydomain.com/blog
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} ^blog.mydomain.com [NC]
  RewriteRule ^(.*)$ http://mydomain.com/blog/$1 [R=301,L]
</IfModule>

I also tried the above directive directly in apache2.conf file but with no result.
When I try to go to the blog.mydomain.com 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.

11 comments
  • Hey. It seems you are actually lost :]

    Why don’t you just remove the A-record that points your sub-domain to your IP and then remove any directives in the Apache config for this sub-domain.

    Once done, just replace any link within your website to point to www.example.com/blog in place of blog.example.com. Just make sure that this “blog” is located in the root directory of your web-server files!

    Hope this helps!

  • 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 blog.domain.com/whatever-post redirects to domain.com/blog(no need for post to post mapping).

  • 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.

  • Are you sure mod_rewrite is enabled?

    sudo a2enmod rewrite
    sudo service apache2 reload
    
  • 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.

  • Show 6 more comments

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.

×
1 answer

This question was answered by @jesin:

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

<VirtualHost *:80>
   ServerName blog.mydomain.com
   Redirect "/" "http://mydomain.com/blog/"
</VirtualHost>

View the original comment

Submit an Answer