DNS records & .htaccess to catch & redirect subdoma

Hi all,

I'm having some problems wrapping my head around the proper solution to this problem and hopefully some of you with more experience can help. If you don't want to read all of this just jump down to the Question heading to get to the point.

Background

First, here's my setup: I have a linode that hosts a couple of domains, say example.com and example.org. Currently both have subdomains of mail.example.com and mail.example.org with CNAME/MX/TXT records for redirecting to Google Apps mail and they have A records for www as well as blank. As well, example.org has a couple other subdomains for non-http purposes, such as ssh.example.org, with A name records for each.

Both have an .htaccess file in the root web directory that consists of, amongst other things, the following code to redirect eg. www.example.org to example.org:

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

So if I go to www.example.com it changes the address in the bar to example.com.

All of this works fine. However, I want to make some changes and I'm unsure how to proceed.

The Question(s)

What I want is to set it up so that I have all subdomains EXCLUDING mail to be directed to my webserver, with mail still going to Google, and then for anyone coming from any other subdomain (eg. www.example.org or ssh.example.org or whatever.example.org) to have the address rewritten to example.org. However, to make matters slightly more complicated, on example.com I have a few subdomains that I want to maintain the subdomain in the URL and to redirect the webserver to subdirectories of my site's root directory. So, for example, going to store.example.com should display store.example.com in the address bar but redirect people to example.com/store. Alternatively, if required, I'd certainly be open to having this as a completely different site in apache, whatever's considered best practice.

How to proceed

First, DNS. I believe that I need to add a wildcard (*) record in DNS manager, but should this be an A record or CNAME record? I've been getting conflicting answers, all claiming to be authoritative, from searches. Should I then get rid of the blank entry and the www entry or, conversely, should I add an entry for store? And will this interfere with redirection to Google apps? Everything I've read seems to say that wildcards will only come into play if other records for the subdomain aren't found, but the Wikipedia entry for the topic says that the standard is cryptic and implementations vary, so I'm not sure.

Then there's the matter of how to handle things at the server. Should I have a separate site set up in Apache to catch store.example.com, as I mentioned above, or can I use the .htaccess file to redirect & rewrite things?

Is there anything else that I'm missing?

Sorry about being so long winded, I'm trying to be thorough. Any help that anyone can give is greatly appreciated!

TIA!

6 Replies

So I think I've found the solution(s). I'll post here for anyone who might be looking to do the same.

First, I added an A record in DNS Manager for both domains of a wildcard (*) that directed to the IP of my Linode. That seems to be the consensus on how to do this. So that got all subdomains going to my linode. I didn't remove www, blank, or add store.

Next, I altered the /etc/apache2/sites-available/example.org and example.com files, adding SiteAlias values of www.example.org example.org *.example.org , and the addition of specifically mentioning store.example.com in the SiteAlias for example.com (not strictly needed, I don't think, but then neither should www be needed). Then I reloaded Apache.

Next, I altered my .htaccess files for each domain. example.org is as follows:

Options +FollowSymLinks
Options +Indexes

#remove subdomain in url
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/$1 [L,R=301]

For example.com I chose to simply redirect store.example.com to example.com/store. I think I could have added a new site in apache that had my subdirectory as it's root but this seemed simpler. At any rate, here's the .htaccess for example.com:

Options +FollowSymLinks
Options +Indexes

#remove subdomain in url
RewriteEngine On
RewriteBase /
#Ignore example.com
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
#This condition will also ignore store.example.com
RewriteCond %{HTTP_HOST} !^store\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

#Redirect store.example.com to subdirectory /store
RewriteEngine On
RewriteCond %{HTTP_HOST} ^store\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/store/$1 [L,R=301]

So this works almost perfectly. With the mail subdomain set up with the correct records (ie. I just followed Google's instructions during my initial setup and I didn't have to change it) it redirects to Google without a hitch. I say it everything almost works perfectly because there's one subdomain that still won't work. It has to do with my hostname, I believe, as hostname.example.com (which is my linode's FQDN as shown in hostname –fqdn). Any ideas as to why this would be?

I'm a little confused on your question, if you setup a CNAME record, or a A record for a subdomain, it will route to that DNS Entry -before- apache can even see it.

This means in order for mail.mydomain.com to work, all you have to do is put the right A address, or CNAME record for that subdomain. You don't need to worry about it ever reaching your server.

So your saying your hostname, for example.. is "test", and your website is "test.com".

The redirect isn't working when typing "test.test.com"?

@superfastcars:

I'm a little confused on your question, if you setup a CNAME record, or a A record for a subdomain, it will route to that DNS Entry -before- apache can even see it.

This means in order for mail.mydomain.com to work, all you have to do is put the right A address, or CNAME record for that subdomain. You don't need to worry about it ever reaching your server.

So your saying your hostname, for example.. is "test", and your website is "test.com".

The redirect isn't working when typing "test.test.com"?

As mentioned in the second post I already solved this, with one minor glitch that didn't clear up until recently (see below). I already knew that the mail entry would redirect to Google. That was never an issue. The problems were:

1. I wanted to know the proper way to set up the DNS records - ie. CNAME or A record, and whether to leave www and blank or remove those entries. Technically it should work with either CNAME or A record, and you shouldn't need www or blank as * will catch them anyway. But I wanted to know the proper way of doing it as that's important to me. It seemed to me, as I believe it does to you, from reading into your comments, that all non-canonical domain names should get CNAME records with only one A record necessary per domain. But the concensus I got elsewhere was that they should all be A records. Which seems wrong but I went with it.

2. I wanted to know how to set up .htaccess to rewrite all domains EXCEPT store.example.com to example.com. Which I figured out myself.

The issue that then came up was that it wouldn't rewrite my Linode's FQDN (ie. hostname.example.com) to example.com. So going to blahblah.example.com will rewrite to example.com but going to hostname.example.com would remain hostname.example.com. Everything else was fine as outlined above. It seemed like an obvious Apache issue but it eventually went away on it's own within 24 hours with no other changes (or reboots) to the linode, so it had to have been a DNS oddity.

Thanks!

@JoeBean:

… and you shouldn't need www or blank as * will catch them anyway.

Wrong. Yes, * will catch www, but it will not catch blank (if by "blank" you mean example.com., as opposed to www.example.com.). You'll have to create a separate record for that.

@mnordhoff:

@JoeBean:

… and you shouldn't need www or blank as * will catch them anyway.

Wrong. Yes, * will catch www, but it will not catch blank (if by "blank" you mean example.com., as opposed to www.example.com.). You'll have to create a separate record for that.
Ok, good to know. I figured being a wildcard it would catch anything or nothing, but I guess it makes sense that it wouldn't.

So to summarize you would need at minimum a blank entry and a wildcard (*) entry.

@JoeBean:

Ok, good to know. I figured being a wildcard it would catch anything or nothing, but I guess it makes sense that it wouldn't.
It depends on your definitions of "anything" and "nothing". :D

I'm not great at explaining things, and this is a bit complicated, but I'll give it a try.

Wildcards respond when these two conditions are in effect: 1.) The queried name is at the same level as the wildcard, i.e. not a subdomain or parent, and 2.) no other records, of any type, exist for that name. As an example, if you have a DNS zone like this (simplifying):

$ORIGIN example.com.
@    SOA ns1.linode.com. joebean.example.com. 2011...blahblahblah
@    NS  ns1.linode.com.
@    NS  ns2.linode.com....blahblahblah
*    A   192.0.2.4
mail MX  10 mx1.example.net.
www  A   198.51.100.200

A query for an A record at "example.com." would return nothing, since "example.com." has no A records and is not covered by a wildcard.

A query for an A record at "foo.example.com." would return 192.0.2.4.

A query for an A record at "bar.foo.example.com." would return nothing, since the wildcard doesn't cover "*.foo.example.com."

A query for an A record at "mail.example.com." would return nothing, since "mail.example.com." has an MX record and is thus not covered by the wildcard, but it does not have any A records.

A query for an A record at "www.example.com." would return 198.51.100.200.

It's also worth noting that a wildcard for "*.com." would not cover "example.com.". For one thing, I assume you aren't a sysadmin at VeriSign, the operators of the "com." zone – and if you are, you don't want to be fired. ;-) Secondly, even if you were looking to retire with a bang and did create such a wildcard, "example.com." has NS and SOA records, so the wildcard wouldn't apply to it.

@JoeBean:

So to summarize you would need at minimum a blank entry and a wildcard (*) entry.
Yup.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct