February 4th, 2010 §
Sometimes, albeit rarely, I will need to move all my posts from one WordPress blog to another one. This is a daunting task, but it does not need to be.
WordPress and .htaccess actually make this very easy! In three easy steps, you can have all of your WordPress content moved from your original blog location to your new one.
Side Note: This blog post assumes you have set up your new website and installed WordPress (2.9.1 or better) there already. If you need to, upgrade your WordPress installation before going on.
1. Export all of your blog posts
With WordPress you have access to a tool for exporting all of your content into an XML file format. Do not worry about what XML is, or why it exports to this format. What you need to know here is how to export and where the file saves, let the rest be taken care of by WordPress.
To get to the export tool go to the left sidebar and click the downward arrow next to tools like so:

Click the arrow here
The menu will look like this when it expands:

Expanded Tools Menu
Now click the “Export” link, and you will see the following:

The Export Page
You can choose which author you want to export, but for our purposes we are moving ALL the content, so we chose to leave it on “All Authors”. When you hit the download button, you will be prompted to open or save the file. Just save the file in a location on your computer where you can find it easily.
2. Import all of your blog posts
Once you have an export file saved to your computer, you are ready to go back to the same Tools Menu from above, but on your new blog site. Click on “Import”. Here, you will see a page that looks like this:
Since we are going to import from a WordPress blog, click the blue “WordPress” link at the bottom to see this page:
Just like a normal upload form, click the browse button to choose the file you downloaded in the preview step. Then click “Upload and Import”, which will allow your new website to pick up the posts you exported.
You may get an option on the next page, which will say “Import and Download all attachments”, DO THIS. It will allow WordPress to download the attachments in each of your original posts from your old server and put them on your new server. This will let you keep the great content you worked so hard to create the first time.
The last option you get is what user to import all the posts as. Go ahead and choose the username you want all of your posts to show up under.
Once you see all the posts are on your new site, you are ready to make sure that ALL traffic from your old domain go to your new one.
3. Setup .htaccess on the old blog to redirect all your traffic
The .htaccess file is a little file that works in the background on your server. It is typically used to tell the server some special action to perform whenever a page is requested. In this case, we are going to tell it to redirect ANY traffic from the old domain to the new domain and make sure the URLs are similar. This is important because if someone goes to your post about how delicious spam is on the old server, you want them to go to the same post on the new server.
You can actually just download an example .htaccess file with the link below. You will need to edit the part that says “old domain” and the part that says “new domain” to be your old and new domains, respectively. The rest of the code in the file is exactly the same as what WordPress has you put in by default.
Example .htaccess file
You will actually edit the .htaccess file that comes with WordPress to have this extra content (this is in the example download above):
-
Options +FollowSymLinks
-
RewriteEngine on
-
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
-
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
DO NOT WORRY about the other code, just find this block and change the URLs appropriately.
The final code will look like this:
-
# BEGIN WordPress
-
<IfModule mod_rewrite.c>
-
RewriteEngine On
-
RewriteBase /
-
-
Options +FollowSymLinks
-
RewriteEngine on
-
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
-
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
-
-
RewriteCond %{REQUEST_FILENAME} ^wp-content.*
-
RewriteCond %{REQUEST_FILENAME} ^wp-admin.*
-
RewriteCond %{REQUEST_FILENAME} ^wp-include.*
-
RewriteCond %{REQUEST_FILENAME} !-f
-
RewriteCond %{REQUEST_FILENAME} !-d
-
RewriteRule . /index.php [L]
-
</IfModule>
-
# END WordPress
-
-
Options -Indexes
Once you have the file edited with your new URL, just upload it to your OLD server so that it replaces the old .htaccess file.
Now, try to go to the old website with any blog post link (search Google for pages from your site), and see what happens to the URL when you go there. You should see the URL change to the new server’s domain with the same post path as you selected.
You are now set up and ready to go on your new server.
Sphere: Related Content
November 12th, 2009 §
I really wanted to use WordPress Mu for Focus Of The Week, which is a site where I give people blogs and they post on their topic once a week.
WordPress Mu is a multi user platform for WordPress where you can manage multiple blogs from the same administrative area. It is a great tool for managing multiple blogs, and on DreamHost it would be perfect! However, they do not have a way for you to do dynamic DNS unless you are paying extra for DreamHost PS, so there is a little extra work that goes into setting it up.
So how do we do this? Follow these steps and you are on your way:
First, make sure your domain is propagated to your DreamHost server. Second, you need to make sure that WordPress Mu is on the server and installed. You can do this pretty easy with the installation instructions and a little elbow grease if you haven’t done it before. It should not take you longer than 20 minutes, if it does you might want to phone a friend.
Now that Mu is set up, you may have noticed the Blogs administration area under “Site Admin”:
Add a blog to the next page by filling out this form:

Once that is in the system, you will notice if you go to that domain that you cannot see the blog. You still need to set up your DreamHost DNS to know about the domain. The easiest way to do this is through the DreamHost Panel, NOT AT YOUR REGISTRAR. The reason is simple, DreamHost already has the DNS for your main domain, so all requests are going to them, even for subdomains. Also, when you set up the subdomain with them it will propagate so much faster than with your registrar.
You do this through the DreamHost Domain Manager:
Once you submit the domain, you just have to wait a little while. Sometimes it is really fast and sometimes it takes up to a couple hours. What I do is just check the URL in my browser every half hour to an hour.
You can now host a WordPress Mu site on DreamHost!
If you need hosting for a blog site, DreamHost is a pretty good way to do it. For a single blog, they have One-Click-Installs which makes installing regular WordPress a breeze. To get a huge discount, $50 off any normal hosting fees you can use Promo Code: PROJECTLAUNCHER
Sphere: Related Content
October 27th, 2009 §
September 19th, 2009 §
I had a friend ask me recently about using the Twitter API with PHP and if I could show him a way to do it. So, this is the first post of my tutorial on how to use the Twitter API. The tutorial will show you how to get a list of your friends and followers, with their statuses. Also, this tutorial assumes you have an environment with Apache and PHP running.
I am making some specific decisions with these posts:
- I am going to use the Zend_Service_Twitter class because it makes things easy
- I am NOT going to put this in full MVC, because this is for beginners and because that is alot of overhead for what I am trying to do
The Twitter API is a fairly well featured API which is easy to use, especially when you have the right tools. One of the tools that I will often use, is the Zend Framework Twitter Service class. The reason that I do this and don’t write all the code myself is, “Why skin the cat again?” – it’s cruel and unusual punishment to skin the cat the first time…
First thing’s first though, we need to set up the environment so that we can both use the Twitter API and use the Zend Framework classes without having to do too much extra work.
The Zend_Framework is an OOPHP framework with a specific naming convention for its classes. Each of the names of the classes can be mapped straight to the folder path for those classes. If your class was called A_B_C the file path would be A/B/C.php. You can handle this string conversion in PHP this way:
$string = 'A_B_C';
-
//replace the '_' with '/' in the class name
-
$path = str_replace('_', '/', $string);
So, now that we know how to convert the string, we need to get the class names as they are instantiated.
A class in PHP is instantiated using the new operator like so:
-
$classVariable = new Class_Name_Here($params);
Where:
- $classVariable is ANY variable name, typically you will name this appropriately to the class name
- ‘Class_Name_Here’ is the name of the class you are calling
- $params are the parameters you want/need to pass to the class constructor. (Some classes don’t require this at all)
When a class is instantiated, PHP will do a couple things. First, it will look to see if there are any class definitions that are local to the current scope of the script that is being run. This will ONLY happen if the class file is included, or the class is defined in the current script. Second, PHP will look for the __autoload magic function. This function will receive the name of the class that is being called and will give the programmer a chance to include the script that has the class. This is why Zend names their classes the way they do. The __autoload function we will use is here (notice the code from above):
-
function __autoload($className)
-
{
-
$path = str_replace('_', '/', $string).'.php';
-
require_once $path;
-
}
I used “require_once” here because you don’t want to include the same file multiple times, this can cause issues and is not worth the trouble or the time to troubleshoot. Also, note that I appended ‘.php’ to the end of the string, this is necessary as we are including a php script. The code above includes the class file and makes the class definition local to the current script so the object can be created. You will need this function available to every script that uses classes.
The last thing that we need in order to pull in classes dynamically as I have described above is a proper include path. The PHP include path is simply a base file path from which the server will look to see if files being included are found. So, in our example above where we have a class called “A_B_C” who has a file at path “A/B/C.php”, without an include path set the “A” directory would have to be in the same directory as the script you are running. When you are writing software you often want to compartmentalize (Read: Organize) your code into directories, which means that you will NOT have the “A” directory available and in the same directory as every script. This again, would be cruel and unusual punishment to you and anyone who works on your code after you. So, you define a place where you want to put ALL of your class files and then set up the include path to point to this location. The easiest way for a beginner to set the include path, is to pass it through a PHP system function called set_include_path. This will set the include path for the current run of the script you are using. This is what it will look like:
-
//the root path of your server, to the execution directory for the server
-
$rootPath = $_SERVER['DOCUMENT_ROOT'];
-
//set the include path with the current include path and the path to your class files
-
set_include_path(get_include_path() . PATH_SEPARATOR . realpath($rootPath.'/path/to/class/files/'));
You will find the document root of your server and set the include path by appending your class file path to your current include path. Now, whenever you include a file, PHP will look in your current directory, and any directories in the include path. The “realpath” function will cleanup any issues in the path like double slashes.
So, I know that this has been quite a lot and seems a little bit like “Seriously Matt, I just want to get information from Twitter, why are you telling me all this?”. I understand that this is true, but it is important to know WHY things work the same way that it is important to know THAT things work. I believe that when you know the why, you can understand what you are playing with and use it to its fullest potential instead of chalking it up to magic (PHP Magic functions excluded — nerd ?humor?).
Where has this gotten us? The following code is what you will need to have a PHP environment that is conducive to dynamically loading classes and using the Zend classes, so you don’t have to re-write all that code.
-
//the root path of your server, to the execution directory for the server
-
$rootPath = $_SERVER['DOCUMENT_ROOT'];
-
//set the include path with the current include path and the path to your class files
-
set_include_path(get_include_path() . PATH_SEPARATOR . realpath($rootPath.'/path/to/class/files/'));
-
-
/**
-
* Sets up the PHP functionality for dynamically loading classes based on class name
-
*/
-
function __autoload($className)
-
{
-
$path = str_replace('_', '/', $string).'.php';
-
require_once $path;
-
}
Having this code means that the next step is to use the Twitter API and avoid wasting time on skinning that damn cat.
Sphere: Related Content