My Apache administration skills are something that I am working on – ongoing. Furthermore, any IT / Web / Linux admin worth their salt should have somewhat of a handle on DNS and on Apache. I have somewhat of a handle on both, though like most things I am not an expert :), rather I am always a professional generalist (always getting a little better at everything…). So when a client came to us with a scenario recently I had to do some searching around to figure out what the best solution was. I am not stating that this IS the best solution or best practice however it is working well for us and I would love to hear some ideas, if you have them, on how you would have solved this.
So basically our client built a site. Let’s call it widgets.com. However, after this site was around for several years and garnered some decent traffic they realized they really wanted it to be called coolwidgets.com and they were rebuilding the thing anyhow. That being said, they didn’t just want to outright drop “widgets.com” as, hey, that is getting some traffic and people think of this client as both “widgets” and “cool widgets” so they would really like to keep both names but always redirect to coolwidgets.com.
Just to be clear, the sites are the same (same server, same public IP, etc…). Furthermore, they want a 301 redirect (this is exactly what they requested) and not just a rewrite.
The DNS configuration was the following:
widgets.com has a CNAME record pointing to coolwidgets.com
coolwidgets.com has an A record pointing to the public IP address for the server hosting the site.
If you are looking for a short and excellent explanation of CNAME records: http://en.wikipedia.org/wiki/CNAME_record
The DNS configuration was/is correct and it enables bot URL’s to resolve to the same IP address.
Well, at first I tried modifying their widgets.com apache site config file with something like the following:
ServerAlias www.coolwidgets.com
ServerAlias widgets.com
ServerAlias www.widgets.com
Redirect permanent / http://www.coolwidgets.com/
Now, you Apache admins out there that are more experienced than I know exactly what happened… don’t you….
Yep, I ended up with a redirect loop… Why?
It was rather obvious once I stopped and thought about it. I am redirecting any address handled by this VirtualHost file back to http://www.coolwidgets.com… which is handled by this virtual hosts file and around and around we go…
So what was the work around?
At first I considered using a rewrite rule but the client really wanted a 301 Permanent Redirect to help with Search Engine stuff, even though the site was still the same stuff in the same place just with a different name.
Then it occurred to me what I needed to do… I needed to host widgets.com as a separate site. What?
Yes, they are the same site, but for a redirect rule to work they need to be different sites. They are on the same server and IP though aren’t they? Yes… but apache can differentiate between two sites on the same IP/PORT using host headers (which is done ALL the time on shared hosting plans). For each site/virtual host file you simply put in the ServerName directive. I also created a new blank web folder for the virtual host file for the site to refer to. I am not sure if that was necessary but I wanted to avoid any chance of Apache throwing an error when I restarted the service.
So I copied my existing virtual host file (after cleaning it up) and modified it for a widgets.com standalone site pointing at a blank directory. I put the 301 Redirect rule in there and it worked perfectly. In the end, my two configs looked something like this:
The MAIN active site:
ServerAdmin webmaster@localhost
ServerName coolwidgets.com
ServerAlias www.coolwidgets.com
DocumentRoot /var/www/coolwidgets
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/coolwidgets/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
The shell old site name redirecting to the new site name.
ServerAdmin webmaster@localhost
ServerName widgets.com
ServerAlias www.widgets.com
Redirect permanent / http://www.coolwidgets.com/
DocumentRoot /var/www/widgets-old
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/widgets-old/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
Between the DNS CNAME configuration and the above configuration for two sites using a second Virtual Host, the redirect is now working beautifully! Hope this helps someone else out!
Regards!