Doing .htaccess Redirection

by on September 10, 2006

in Internet

I’ve been rolling up my sleeves playing with server stuff directly for the
first time in ages, and I wanted to redirect any requests for any files within a
particular directory of my consulting site Calafia
to a new location. Time to do some learning.

I’m dealing with Apache, so the
basic
documentation
on using redirection was helpful. Within a minute, I had any
requests for something like http://olddomain.com/olddirectory going to
http://newdomain/.

The problem was, any requests for a particular file were trying to locate the
same file name at the new domain. In other words,
http://olddomain.com/olddirectory/fileabc.html would bring up
http://newdomain/fileabc.html. Since the new domain had no fileabc.html, errors
started happening.

Back to the research. What I really needed was a way to redirect all files
within a folder to a single location. I did a search, and surprise! It’s Aaron
Wall’s SEO Book site to the rescue.
.htaccess, 301 Redirects
& SEO: Guest Post by NotSleepy
covered how to rewrite all files within a
folder to a single file. Once I had that down, I redirected requests for that
single file to the new domain. Voila — all done. And don’t I feel cool having
done a rewrite! My code, by the way:

RewriteRule ^directoryname(.*)$ /newfilename [L,R=302]
Redirect /newfilename http://newdomain.com

I’m sure there’s a better way to do this (if so, just say below in comments),
but it worked, and I didn’t have to ask anyone!

{ 3 comments… read them below or add one }

1 Matt Cutts September 10, 2006 at 10:45 pm

Just out of curiosity, why a 302 instead of a 301?
I haven’t been into my .htaccess file since I was doing password protection a year ago, so you’re a braver man than me. ;)

2 JEHochman September 11, 2006 at 5:53 am

I don’t know if this is better, but it’s simpler.
RedirectMatch 301 /directory/.* http://www.newdomain.com/

3 Danny Sullivan September 11, 2006 at 1:17 pm

Hey Matt, went 302 temporary redirect because I don’t know permanently yet where want to point those old files yet. Once I do, I’ll go 301.
JE, excellent, thanks! Worked great and switched to it with one change:
RedirectMatch 302 /directory.* http://newdomain
I found what you suggested worked except if someone entered http://olddomain/directory without the trailing slash. I suspect that what I’ve done is made any thing on my server that starts with that directory name now redirect, but it’s a unique name, so I’m OK.

Leave a Comment

Thinking of dropping your link spam? Consider this. Seriously, STOP & READ. The guy who runs Google's spam fighting team? I know him pretty well. In fact, it's sort of a joke between us to see what's the latest absurd link drop I can share. So if you want your site to be a poster child on his idiots wall -- and probably to encounter a Google penalty -- go ahead, drop your link. It's nofollow anyway, plus I do have built-in spam fighting and what gets past that usually gets nabbed in a few minutes to a few hours. So you got to ask yourself. Are you feeling lucky?

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Previous post:

Next post: