Adventures in Apache .htaccess Redirects
I came across an interesting problem today that I thought I would share. I’ve recently built my new website chrysaliswebdevelopment.com and I decided I need to take my old website eddieolivas.com down temporarily while I update it. So I made a little under construction graphic to post on eddieolivas.com 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 eddieolivas.com/anything, for example of someone clicked a link on Google to my old content like eddieolivas.com/pics, it would redirect back to eddieolivas.com 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 eddieolivas.com. Sounds simple right? Don’t I wish!
The Problem:
My problem was that eddieolivas.com is my root domain and chrysaliswebdevelopment.com 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 .+ http://www.eddieolivas.com/ [R=301,L]
At first this seemed like it was working great. I could go to eddieolivas.com/whatever_anything and it would correctly redirect to my new graphic at eddieolivas.com. However, I then found that when I went to chrysaliswebdevelopment.com this is what I was getting:
I’m assuming that because chrysaliswebdevelopment.com is a sub-folder within eddieolivas.com, this new .htaccess code breaks WordPress and makes it show the folder listing for the root of chrysaliswebdevelopment.com. 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 JRGNS.net 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
http://yourhost.com/some/path/somewhere
will result in
http://yourhost.com/index.php?first=some&second=path/somewhere"
Confused yet? Join the club! Basically this code tells me how to break up the incoming URL request into two pieces, the eddieolivas.com part and the /whatever_anything part.
So to get to the point, if you ever need to redirect all requests for somedomain.com/anything back to somedomain.com while excluding a sub-folder within somedomain.com 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.)?eddieolivas.com$
# Don't apply when request is the root domain RewriteCond %{REQUEST_URI} !^/* [NC]
# Rewrite request to eddieolivas.com/index.html. RewriteRule ^(.*)$ http://eddieolivas.com/ [R=301,L] RewriteRule ^([^/]*)(.*)$ index.html
Conclusion:
With that code in place I finally got what I wanted. All requests to eddieolivas.com/anything_at_all redirect to eddieolivas.com and chrysaliswebdevelopment.com 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.