Tag Archives: redirect

WordPress: Switch to a New Domain by Setting a Permanent Redirect 301 via .htaccess CodeUnit 30 JUL 2012

Deciding that I really don’t have the desire nor free time to support multiple websites across multiple domains, I’ve started the process of copying all the content over from my unwanted WordPress sites to my primary WordPress site (at http://www.craiglotter.co.za) and then removing the unwanted installations. (Note: The WordPress Export/Import tools found under Tools work pretty damn well.)

However, because my unwanted sites still carry some decent link juice, SEO and SERP scores, I don’t necessarily want to lose all of that search engine goodness, and so set about setting up permanent redirection (301 HTML header) notices using the .htaccess file in the old accounts.

What this does is tell any search spider or browser where the content now sits, as well as the fact that it is a permanent redirect, meaning that hopefully the search engines update their indexes accordingly. Anyway, that’s the plan, but nevertheless, at least if you click in via an old link, you will still get transported to the desired content, albeit on a new domain! :)

To achieve this, first open your .htaccess file which you will find in the root of your WordPress installation directory. Note that this is a hidden file so you’ll need to edit it either via an FTP client or some other interface which allows you to list hidden files.

Once opened, be sure to remove all the WordPress rewrite rules which can be located between the “# BEGIN WordPress” and “# END WordPress” tags. Next, populate the .htaccess file with the following text at the top of the file:

<IfModule mod_rewrite.c>
# Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.craiglotter.co.za/$1 [R=301,L]
</IfModule>

# BEGIN WordPress
# END WordPress

Save the file and then try to hit an old URL like “http://www.codeunit.co.za/2010/01/16/php-simple-try-catch-example/” and note your browser automatically heading off to “http://www.craiglotter.co.za/2010/01/16/php-simple-try-catch-example/”.

Nifty.

PHP: Page Redirecting with Header… and Die() CodeUnit 25 OCT 2010

php elephant iconI make use of PHP’s handy header function to handle simple page redirects for me, perhaps a bit of a cheap way of doing it, but one that is remarkable effective. Sure, you need to change the way you code a little and perhaps introduce some ob_start and ob_end_flush calls if necessary, but on the whole a good old header(‘Location: newpage.htm’) call works pretty damn well.

But here’s a handy note, something to keep in mind when redirecting to a new page using the header function.

Sometimes, somehow, somewhere, a browser script might/will/does continue silently running even after you called your header relocate function, introducing some potentially hazardous side affects because some or other critical session variables or something or other have been set to something they simply weren’t meant to be.

Often this is noticeable with login processing backend scripts, where logins will mysteriously work or fail depending on your set up, occurring purely because session variables have been set when you didn’t think they would be because according to you, the page had already redirected.

So a simple solution to this?

Well the reliable little die() statement works pretty damn well in this case. So the resulting code?

header('Location: newpage.htm');
die();

A simple tip, but one worth remembering.

Related Link: http://php.net/manual/en/function.header.php