How to redirect files to a subdomain using apache mod_rewrite and .htaccess
Recently I re-organize my site and created subdomains for each section of my site. I did this to make my site easier to manage as well as to boost my search engine rankings. This effort required me to move my web pages and supporting files to a new location on my server. When content is moved the url used to access it will break so any bookmarked pages or any search results pointing to that content will be broken. This will result in users not being able to access your site, a negative user experience and will cause lower page rank in search engines. With this in mind I wanted to make sure that the users accessing my site were not affected by the re-organization.
To do this I decided to use redirects. There are many ways to implement redirects but I choose to go with the apache mod_rewrite feature. Most hosting packages include the apache mod_rewrite software such as the 1and1 business linux hosting package. The mod_rewrite feature provides you with the ability to dynamically redirect requests for moved content to the new location.
In my case, I had several subdomains that point to various sections of my site and I could not just redirect all requests to the new location. I had to add logic to exclude requests for certain subdomains from being redirected. For example, I had three domain names including www.ghhutch.com, webdesign.ghhutch.com and blog.ghhutch.com. I wanted all requests for content in www.ghhutch.com to get redirected to webdesign.ghhutch.com but I didnt want requests for blog.ghhutch.com to be redirected. To do this I created a .htaccess file, saved it to the root folder of my website and added the following code.
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^webdesign\.ghhutch\.com
RewriteCond %{HTTP_HOST} !^blog\.ghhutch\.com
RewriteRule (.*) http://webdesign.ghhutch.com/$1 [R=301,L]
The lines beginning with RewriteCond use regular expressions and server variables to prevent requests beginning with webdesign.ghhutch.com and blog.ghhutch.com from being redirected. The line beginning with RewriteRule is the location where all requests will be redirected to if they meet the rewrite conditions. Notice that I added a line to not redirect request for webdesign.ghhutch.com to webdesign.ghhutch.com. This will prevent an infinite loop from occuring which may cause your server to crash.
Using mod_rewrite is very powerful if used correctly but can also be dangerous and get messy if used inproperly. Its always best to take time to design a scalable information architecture so that you dont have to use redirects. When the time comes to re-design your site, archive content or just move things around take some time to think about the impact that it may have on your users. If there is an impact on your users mod _rewrite may be the right solution for you. Using mod-rewrite effectively can save you alot of time and energy as well as provide you with a seamless way to implement a content move.






