Doing .htaccess Redirection

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!

By Danny Sullivan on Sep. 10, 2006 | Permalink
See related posts in: Internet

Next Post: Inventor Of My Aeron Chair Dies
Previous Post: But Wait, There's No More
All Posts: Daggle Archives
Posts By Category: Daggle Categories
Return To: Daggle Home Page

Comments

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. ;)

Comment by Matt Cutts Author Profile Page | September 10, 2006 10:45 PM

I don't know if this is better, but it's simpler.

RedirectMatch 301 /directory/.* http://www.newdomain.com/

Comment by JEHochman Author Profile Page | September 11, 2006 5:53 AM

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.

Comment by Danny Sullivan Author Profile Page | September 11, 2006 1:17 PM

Want to comment? If you are signed into TypeKey, you'll see a form below. No form? Click on the sign-in link below, and you can sign-in or sign-up for a free account. Sorry you have to use TypeKey, but I use it to avoid comment spam. All comments currently appear automatically after posting.

Leave a comment

Subscribe!
Subscribe Via Web Feed
Subscribe with Google
Add to My Yahoo!
Subscribe with Bloglines
Add to netvibes
Subscribe with Live.com
Subscribe in NewsGator Online
Subscribe in Rojo

Add to My AOL

Get new entries via email. Enter your address below:


follow dannysullivan at http://twitter.com
Search