vineri, 10 decembrie 2010

Day 6: Pretty Permalinks and How To Keep Old Links Alive

Have you ever looked at the URLs of your posts and thought “Hm, that might be a bit hard to remember” or even “I wonder what all that stuff means”? If that sounds familiar it’s high time you learned about permalinks and how to make them “pretty”. On this Sixth Day of WordPress we’ll take a look at how to get WordPress to use so-called “pretty” permalinks and also how to change the permalinks of existing WordPress sites without losing old links pointing to the site in the process.


Note that though this is fairly basic stuff that should work fine on most WordPress installations and hosts, I have encountered one case in which messing with the permalink structure had truly horrific site-destroying consequences. The following comes with no warranties and I take no responsibility if something goes wrong when you follow these instructions. That said I do what is described below on all the sites I work with and apart from that one very strange case on a very strange server I have never had any issues.


To understand pretty permalinks and how to make good use of them you first need to understand what they are and how they work. If you already know this you can skip this section and move on to the next one. If not, I’ll try to be concise and educational.


When you create a post or page in WordPress you are actually just creating a database entry equivalent to one row in a spreadsheet. Each post or page is assigned and identifying number so it can be tracked down and called by WordPress at any time. As an example this post has been assigned the number 1040. To call the content from one such entry, all you have to do is tell WordPress to display the content of row 1040 like this: http://www.designisphilosophy.com/?p=1040. This is called a query and it is at the core of WordPress’ functionality.


The problem of course is that whereas the server might find it very easy to deal with each post or page as a number, your visitors and search engines can’t really make heads or tails of it. They’d much rather have a proper description of the content of a particular post. This is where the pretty permalinks come in. By setting up a permalink structure for your WordPress site you tell WordPress and the server to assign new semantic (meaningful) URLs to each post and page – usually based on the title. So instead of that weird URL above the permalink URL for this page becomes http://www.designisphilosophy.com/24-days-of-wordpress/pretty-permalinks-and-how-to-keep-old-links-alive/. Not only is this new URL written in English, but it gives a clear description of the post: It belongs to 24 Days of WordPress and it has something to do with permalinks. And that’s what we want.


By default WordPress uses the numbering scheme for all your content. This is not what you want unless your site will only be read by computers. Which I guess could happen. Fortunately changing the permalink structure for WordPress is a 10 second process:

Go to Settings -> PermalinksClick the “Custom Structure” radio buttonEnter the following strip of code: /%category%/%postname%/Click Save Changes

If your server allows WordPress to write to the .htaccess file this is all you have to do and your posts will now be listed as http://www.yourdomain.com/category/postname while your pages will be listed as http://www.yourdomain.com/pageparent/pagename. Done and done.


In some cases the server will not let WordPress write to a file called .htaccess. If so you have to do some manual labour to get everything to work. Good thing is WordPress does the work for you – you just have to copy and paste some code.


If when you followed the 4 steps above WordPress runs into trouble it’ll give you a warning at the bottom of the page that looks something like this:


All you have to do now is copy the code in the box (may be different from what you see above), paste it into the .htaccess file and push it back up to your server. Here’s a more thorough rundown step by step:

Log in to the root folder of your WordPress installation through FTPIn the root instalation you should find a file called “.htaccess”. If so copy it to somewhere on your desktop and make a backup copy called “.htaccessOLD” just in case something goes wrong.Open .htaccess in your text editor.If there is no .htaccess file in your root folder, open your text editor and create a file named “.htaccess”. Note there is no prefix here and the file has no file type. If you save it as “.htaccess.txt” for example it will not work.Go to the Permalinks page in your WordPress installation, find the code as illustrated above and copy the whole bulk of code.Go to .htaccess and paste the code in anywhere.Save .htaccess and  use your FTP client to put it back into the root folder of your WordPress installation.

Now when  you reload your WordPress site it should have pretty permalinks. If it doesn’t you missed a step somewhere. If everything explodes or nothing loads, go back through FTP, remove the .htaccess file, make a copy of your backup file, rename it “.htaccess” overwriting your new broken version and push .htaccess back up to your site through FTP. This should restore everything.


Now that you have your pretty permalinks properly configured you can start customizing them. When you create a new post or page and click either Save Draft or Publish, WordPress generates a permalink for you. If you used my permalink structure above the post will get a URL based on the category and post name (or in case of a page the page parent (if any) and the page name). If it’s a post with multiple categories WordPress will pick the first category alphabetically and use that one so if you want a specific category pick that one first, save the post as a draft and then add additional categories.


Once a permalink has been established, you can change the postname portion of it. You do this by clicking the Edit button that appears directly next to the permalink (which is displayed directly under the post title) and typing in a new title. Keep in mind on the web only standard English letters work and you can’t have any spaces so separate your words with dashes. When you’re satisfied click OK and the new permalink is applied.


WordPress does a pretty good job at defining these permalinks for you but you may want to edit them. For example this post is has the words “Day 6? at the beginning of the title, but since I don’t think people are going to search for “Day 6? hoping to find an article on WordPress permalinks I’ve taken it out.


“But” you may say “my site has been up for years using the old crappy numbering system and thousands of sites link to me already. If I change the permalink structure on my site, all those links will point nowhere. That can’t be good.” And you will be right. If you have an existing site with an old permalink structure – whether it be the default or some strange structure you or your web developer came up with a couple of years ago – and you want to change it you have to insert safeguards that people who visit your posts and pages using the old structure won’t be led astray.


Fortunately, like with most things WordPress, there’s a plugin for that. And it’s called Dean’s Permalink Migration plugin (for a full breakdown check out Dean’s site).


This plugin works on a very simple principle: You provide the old permalink structure for your site and it will redirect anyone using that old structure to the new permalink locations. Once you have the plugin installed (it’s available through the WordPress Plugin Directory and can be installed from within WordPress) and activated, a new option appears under Settings called “PermalinksMigration”. To use the function simply click this option, enter the old permalink structure in the field and click Update Options. Can’t get much simpler than that.


Now if for some strange reason you have multiple different permalink structures you need to redirect there are other more heavyweight plugins available, but I find that in most cases the simplest option is the best one and this is as simple and straight forward as it gets.


This tutorial is part of the 24 Days of WordPress series. If you want to learn more about WordPress and Expression Web check out the Sams Teach Yourself Microsoft Expression Web in 24 Hours series (version 2, 3 and 4), Lynda.com’s WordPress 3.0 Essential Training course and Microsoft Expression Web 4 LiveLessons.


View the original article here

Niciun comentariu:

Trimiteți un comentariu