Adventures in Apache .htaccess Redirects - Chrysalis Web Development

Adventures in Apache .htaccess Redirects

By on Sep 17, 2012 in Web Design | 0 comments

I came across an interesting problem today that I thought I would share. I’ve recently built my new website and I decided I need to take my old website down temporarily while I update it. So I made a little under construction graphic to post on while I work on it:

(By the way I used graphics from Marketing Graphics Toolkit to make the graphic. Very useful tool for web developers.)


The Scenario:

I wanted to make it so that if you go to, for example of someone clicked a link on Google to my old content like, it would redirect back to and just show my graphic instead.  My old website is just too ugly and outdated for me to bear the thought of people looking at it when they’re considering hiring me to develop their website.


So, I want to close people off from accessing it until it’s updated, but I don’t want to move or delete the site and leave people with 404 errors. My web server runs on Apache so I figured I just need to edit my .htaccess file to redirect all requests to Sounds simple right? Don’t I wish!


The Problem:

My problem was that is my root domain and is in a folder within it. So standard redirects are not going to cut it.  At first I tried adding the following to the .htaccess file in the root of my website:


DirectoryIndex index.html
RewriteEngine on
RewriteCond %{REQUEST_URI} !.(png|jpe?g|gif|css|js|txt)$
RewriteRule .+ [R=301,L]


At first this seemed like it was working great.  I could go to and it would correctly redirect to my new graphic at However, I then found that when I went to this is what I was getting:


Am I paranoid for blurring the file names? Yeah probably.

I’m assuming that because is a sub-folder within, this new .htaccess code breaks WordPress and makes it show the folder listing for the root of Right around here is where I started to pull my hair out. I tried every variation of this rewrite rule I could think of, I searched Google until I went blind, hell I even started brushing up on regular expressions, all to no avail.


The Solution:

By some miracle I stumbled upon this article on which had the following bit of code:


RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2


It goes on to explain:


“This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request 


will result in"


Confused yet? Join the club! Basically this code tells me how to break up the incoming URL request into two pieces, the part and the /whatever_anything part.


So to get to the point, if you ever need to redirect all requests for back to while excluding a sub-folder within using Apache’s .htaccess file, you could use the following code and just swap out the domain names:


# Only apply to URLs on this domain
RewriteCond %{HTTP_HOST} ^(www.)?$


# Don't apply when request is the root domain
RewriteCond %{REQUEST_URI} !^/* [NC]


 # Rewrite request to
RewriteRule ^(.*)$ [R=301,L]
RewriteRule ^([^/]*)(.*)$ index.html



With that code in place I finally got what I wanted. All requests to redirect to and still works great!


I figure one day someone’s bound to have this exact same problem with their website and hopefully this article will be helpful to them.

Post a Reply

Your email address will not be published. Required fields are marked *