Yoast: Change domain, and warn your visitors
When you change domains, for instance using my moving WordPress to a new domain guide, you’ll usually redirect your visitors with something looking like this:
Redirect 301 / http://new.example.com/
The only issue with this is that people might not notice that you’ve moved to a new domain, and thus might not update their links to you. As Matt Cutts confirmed a while back that 301 redirects do not pass full PageRank, and they seem to take a hell of a time to kick in these days, that’s somewhat of an issue: we’d rather have people update their links.
The proper redirect
So we want to tell people that you’ve moved to a new domain, but only people who came through old links to your site. And we still want to keep that 301 redirect in place, to preserve as much of the value from the old site as we can.
So, what if we redirected to http://new.example.com/#moved? The search engines don’t care about anything from the hash onwards, so the value of the redirect would still be maintained. We could then use javascript to detect the value of the hash tag, and display a notice to our visitors.
There’s an issue with that as well though: with the redirect 301 / line above, we redirected everything after /, to new.example.com/, so for instance /test/ would be redirected to new.example.com/test/. If we added #moved to the end of that URL, we would end up with new.example.com/#moved/test/ and that wouldn’t work. So we’ll need to make a bit of a fancier redirect:
RedirectMatch 301 /(.*)? http://new.example.com/$1#moved
That should work. Notice though that when you have code in place adding a / to the end of your URL’s if not existent, that code should conserve any hash tags in the URL.
Showing the domain move message
So now, let’s move on to the javascript part to actually show people a “we’ve moved” message, which is actually pretty simple. You create a div with the message you want to show, and put it in your footer, something like this:
<div id="domainmovemsg" style="display:none;"> We've moved from old.example.com to new.example.com, please update your feeds & links! </div>
As you can see, we default it to display: none; so not everybody sees it, and then you add this tiny snippet of javascript, also in the footer of your page, after you’ve loaded the div above:
if (document.location.hash == '#moved') { document.getElementById('domainmovemsg').style.display = 'block'; }
Example code
I’ve coded an example of this with some more styling etc., which you can find here. If you want to see it in action immediately, you’ll have to click this link.
Change domain, and warn your visitors is a post from Joost de Valk‘s Yoast – Tweaking Websites.A good WordPress blog needs good hosting, you don’t want your blog to be slow, or, even worse, down, do you? Check out my thoughts on WordPress hosting!
No related posts.