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 }
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.
I don’t know if this is better, but it’s simpler.
RedirectMatch 301 /directory/.* http://www.newdomain.com/
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.