Proper htaccess structure for redirecting an entire site?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

I am moving a site to a new domain soon and would like to do a 301 redirect to maintain SEO quality backlinks. The URL structures on the new domain will be exactly like the old with a different domain name. This site has close to a thousand or so pages so I am not really interested in writing a new line for each page and permalink in the htaccess. Is there a simple REGEX command that I can use that will cover all the possible pages and permalinks?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

I did a bit more research and came across this

Code: [ Select ]
Rewriterule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]


Will this work? I don't really have anyway to test this..its kinda like I have to get it right the first time!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

It can work, as long as there are no conflicting rewrites higher up in the processing than that one. Once the new server is in place there is really no reason to have the existing server processing any mod_rewrite directives other than that one and possible a RewriteEngine On rule.

If you decide to add a GET variable to the destination URL to track redirects, be sure to use the QSA flag along with that R=301 flag.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Ok cool, i just realized though that I actually need to do this on 2 sites. The second one i need to redirect at a directory level. In other words, I want to redirect oldsite.com/blog/ to newsite.com/blog/ and keep the content in the root of oldsite.com/ where it is. Would the redirect for that look like this?

Code: [ Select ]
Rewriterule ^blog/(.*)$ http://www.newsite.com/blog/$1 [R=301,L]
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

So on the second site, you'll be keeping the majority of the site where it is, but the /blog/ directory will be moving to a new domain ?

Leave a /blog/ directory intact on the existing site with an htaccess in it and do the redirect from within that directory. Doing it this way keeps the server from needing to worry about the blog redirect during every single request and lets it just worry about it when the request gets to that /blog/ directory.

/blog/.htaccess

Code: [ Select ]
Rewriterule ^(.*)$ http://www.newdomain.com/blog/$1 [R=301,L]


When RewriteRule is used in the htaccess of sub-directories, the names of parent directories are not included in the URI mod_rewrite will be working with.

Now when you test this on your server in an unused directory before going live, if it starts adding what looks like physical file paths to your URLs, you'll want to use RewriteBase to let mod_rewrite know where you are.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 97 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.