Setting Up a Catch-All 301 Domain Redirect and Specific Page Redirects
1-877-660-3735

Setting Up a Catch-All 301 Domain Redirect and Specific Page Redirects

Posted by Alex Juel on June 17th, 2009

Filed Under:

Share This Post:

I had to figure out another .htaccess problem the other day, which was about as confusing as my last problem with 301 redirects on dynamic URLs.

This time, we had a client that had a brand new site built. She had a main site, newsite.com and then four subdomains for each city her business now serves. She also had an old site, which was oldsiteaustin.com.

This is where things get a little tricky.

Her new site has articles on the main domain, which are also available on each of the subdomains, so we had to block them to the search engines on the subdomains to avoid duplicate content penalties.

All of these articles were also on the old site she used to have.

Since her old site catered strictly to an Austin audience, we put in a sitewide “catch-all” redirect for that domain to go to the Austin subdomain of her new site, but since the articles on the Austin subdomain were blocked to the search engines, we needed to redirect the articles to the versions on the main site.

After messing around in the .htaccess for a while, it seemed this was an impossible task.

Here is an example of what I had (which is wrong):

Options +FollowSymlinks

RewriteEngine On
# CATCH-ALL REDIRECT TO SUBDOMAIN#
RewriteCond %{HTTP_HOST} ^(oldsiteaustin|www.oldsiteaustin) [NC]
RewriteRule ^(.*)$ http://austin.newsite.com/$1 [R=301,L]

# SEND ARTICLES BACK TO ROOT DOMAIN #
redirect 301 /article/ http://www.newsite.com/article1/
redirect 301 /article/firstarticle http://www.newsite.com/article1/
redirect 301 /article/article2.html http://www.newsite.com/article2/

I could get the “catch-all” redirect to work, but not the article redirects. If the site wide redirect went to the Austin subdomain, so did the articles even though I explicitly directed the articles to the main site.

I tried placing the “catch-all” below the page redirects in hopes that the page redirects would occur before the site wide redirect, but no luck.

So after a bit of searching and questioning on various forums (thanks jdMorgan over at WebmasterWorld), I figured it out.

I was using mod_rewrite for the site wide redirect and but then mod_alias for the page redirects. It turns out that the priority won’t work if you do it this way.

If you use rewrite rule for the site wide redirect, you also have to use it for the page redirects in order for the rules in the .htaccess to occur in the order they are placed.

So this is what it looks like now:

Options +FollowSymlinks

RewriteEngine On

# SEND ARTICLES BACK TO ROOT DOMAIN #
RewriteRule ^article/(.*)$ http://www.newsite.com/article1/$1 [R=301,L]
RewriteRule ^/article/firstarticle(.*)$ http://www.newsite.com/article1/$1 [R=301,L]
RewriteRule ^/article/article2.html(.*)$ http://www.newsite.com/article2/$1 [R=301,L]

# CATCH-ALL REDIRECT TO SUBDOMAIN#
RewriteCond %{HTTP_HOST} ^(oldsiteaustin|www.oldsiteaustin) [NC]
RewriteRule ^(.*)$ http://austin.newsite.com/$1 [R=301,L]

I’ve changed the page redirects to use the rewrite 301 redirect and then placed the “catch-all” below that.

If you’ll also notice, in the first page redirect, I had to remove the first slash in front of the directory for it to work, but then leave the first slash on for the article pages.

So this is how you can set up a site-wide redirect, in addition to redirecting specific pages to new urls.

About 

Alex’s specialty at Inflow is to link it up, wrap it up, get it out there and get results. Alex Juel has experience with clients large and small in a wide variety of industries.

Share This Post:

2 Comments on “Setting Up a Catch-All 301 Domain Redirect and Specific Page Redirects


    Did you remove the robots.txt block on the old content so the search engines could find them an catch that 301? Or was it a meta thing?

    Great work Alex! One of the problems with working in a place that has development staff is I’ve totally lost everything I learned about writing expressions. I’ll just call you next time I need some on one of my own sites. ;-)


    Yeah, it was a meta thing. Going back over this post reminds me of the nightmare that project was. Just so many technical issues going on. It feels great after doing stuff like this and learning something new though.

Leave Your Comment, or Contact Alex Juel Directly