How To Set Up Advanced 301 Redirects
This tutorial is designed to show you how to use the referring URL to micro-target your visitors to specific landing pages or different sites. Now before you go jumping all over me for suggesting something spammy, let me just say there are very legitimate reasons for doing this. Especially since SEO is a curse word over at Digg.
301 redirects using .htaccess to redirect your whole site
Inevitably, some of you are here to redirect your whole site to a new domain name in PHP, so, I am going to throw you a bone. The rest of you can skip down to the next section.
Paste this in the .htaccess file of your old domain.
Options +FollowSymLinks RewriteEngine on RewriteRule (.*) http://www.example.com/$1 [R=301,L]
It is imperative that you not lose any of your link juice. So if you are switching to a new URL structure you can use something like this in your .htaccess file at the new site to quickly redirect each page to the new URL.
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Many do not know about this feature, but Google Webmaster Tools now has a spot to tell them about transferring a new domain. I did it recently and highly recommend you follow the instructions when changing domains.


Basic Page 301 Redirect
Its time to start building our script. Lets start with the basic 301 redirect for a PHP page:
Grab the referring URL
The above snippet of code puts the referring URL into a variable. We can now check the variable and use the information to set up a conditional 301 redirect. So lets parse our new variable for “digg”.
$digg = "digg";
$result = strcspn($ref, $digg);
if ($result != strlen($ref)) {
return $result;
}
return false;
To stick with our previous digg example, the following code (Alpha) is used for a Digg redirect.
I have not tested this script as of yet. I just wrote it, but it should work. When testing is complete I will make a note here.
Related posts:
- Bing Outshines Google In Handling Domain Name Changes I have had the unfortunate experience of changing domain names...
- Introduction to PHP Functions This is an introduction into the world of PHP functions....
You should sign up for my RSS feed here
Broken image from a local file: file:///C:/Users/Jason/AppData/Local/Temp/moz-screenshot.png
.-= Ruud Hein´s last blog ..Insert Adsense in Content in Wordpress =-.
[...] How To Set Up Advanced 301 Redirects [...]
Thanks for the very nice explanation of advanced 301 redirects. I’ll use it.
@Bonnie, let me know how it goes.
Hey Jason,
I’m setting up a redirect from a domain to my subdirectory blog.
But I’m sure that by adapting this a bit I’ll get it done
Thanks for the step by step.
Deliciousing + stumbling it.
All the best,
Eren
.-= Eren@ Embracing Blogging´s last blog ..Free Twitter buttons, icons – Follow me on Twitter images =-.
Thanks for this incredible explanation. I always messed up the 301 redirects in my website and was worried about the way search engines treating bad redirects. Now I think I can do it myself without flaws.
@Eren, I am sure you will be able too, let me know if you run into any problems…thanks for the stumble and delicious.
@Girish, glad you found it useful. 301’s are not that difficult once you have done them a time or two…you can always go back and check them and make sure they are working. Use the original URL and paste it in this tool.
Thanks for the very nice explanation of advanced 301 redirects. I’ll use it.
I love it
I’ve always avoided 301’s thinking it was a difficult area. It seems to be relatively straight forward though. I suppose when you think a about it a lot of large companies use redirects on their sites to send you to the right pages (for example depending on your location).
Jason, thanks for briefing the complex terms associated with 301’s and I really liked the tool you recommended to girish. I checked my website and found out several poky issues, which I am gonna fix without my techie’s help. Thanks to your post which coaxed me to take this decision
.-= Alan@Tulsa Home´s last blog ..Winter and Holiday Home Tips =-.
@Alan, glad I could help.
I’m really not here to learn how to set up advance redirects I thought it be interesting to read this.
.-= Mariah@free nintendo wii´s last blog ..Free Nintendo WII Console =-.
Hmm interesting, but if I am using not a WP, my blogs are on joomla would it work?
It might require a workaround, but in principle it will work
I’d like to transfer my site to a new domain with a main keyword in it. I take thought for the link juice.I’m not sure i can do it right. With the help of this tutorial, i will test this. Thanks!
One word of caution. You should check out this post on how Google handled my attempted domain name change. I ended up changing the domain name back because of how bad of a hit I took with 301 and using the Google Change of Address. http://www.mywebtronics.com/blog/bing-outshines-google-in-handling-domain-name-changes/