My Subdomain will redirect back to the main site. How do I fix this so it redirects to the subdomain? My main site is a WordPress adult site so I'll use example.com to explain. All I have in the subdomain directory is an index.php file with phpinfo().

I have a website at www.example.com. I set up a Virtual host file in /etc/apache2/sites-available and did the command: a2ensite example.com.conf. Then I restarted apache2.

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin admin@example.com
  ServerName  subdomain.example.com
  ServerAlias www.subdomain.example.com
  Redirect / https://subdomain.example.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.php index.html
  DocumentRoot /var/www/subdomain
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/subdomain/log/error.log
  CustomLog /var/www/subdomain/log/access.log combined

  <Location /server-status>
     SetHandler server-status
     Order Deny,Allow
     Deny from all
     Allow from localhost


In DNS tab on Cloudflare, I have an AAAA record and an A record with Subdomain in the name field and the server IP address in the value field. I have entries for the subdomain and also www.subdomain.

  • I change permission so that www-data owns the subdomain directory and now I get a 403 Forbidden error. Anyone know how to fix this? Do I need a .htaccess file? – Olympus Oct 17 '18 at 15:02
  • I was able to ping the site. – Olympus Oct 17 '18 at 15:55
  • I was able to get subdomain.rootdomain.com to work by copying and pasting the -ssl.conf file and enabling it. However, www.subdomain.rootdomain.com doesn't work still. Both are set up in DNS on Cloudflare and I am able to ping both. Anyone know why it doesn't work with www preceding the subdomain? – Olympus Oct 17 '18 at 16:14
  • www.example.com you might want to setup as an alias, but why do you need www.subdomain.example.com ? if subdomain.example.com will be indexed, and use subdomain.example.com in your promotional info then www.subdomain.example.com is superfluous. there's literally no point in having it. – garth Oct 18 '18 at 7:44
  • The question is not very clear! Do you want subdomain.example.com to redirect to subdomain.example.com ? And, subdomain.example.com to redirect to subdomain.example.com ? I think the first one should work as per your Apache configuration, but the second might not work. – anup Oct 18 '18 at 11:04

In Apache's SSL config for subdomain.example.com, add a redirect for https://www.subdomain.example.com:

ServerAlias www.subdomain.example.com #make sure alias is present

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.com [NC]
RewriteRule ^(.*)$ https://subdomain.domain.com/$1 [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.