Make my urls purty!
November 25, 2008 4:01 PM   Subscribe

What's the best way for me to create human-readable urls for a Wordpress site?

So I've taken a job converting a very static site to one that will use Wordpress as a CMS. A couple of key points up front:

1) We are 100% committed to using Wordpress. Thanks for respecting this.
2) I will NOT be making use of any wordpress theme or allowing users to go to any Wordpress pages at all. We are using it as a CMS on the backend, and on the frontend I will write custom php pages and queries to display what i want to display.

The site as it stands now has 1000s of articles in totally flat html pages. We want to get away from that for obvious reasons. My inclination is to create one page to use over and over again, so where the old url would've been like:

www.thesite.net/article_name.html

the new url would look like:

www.thesite.net/article.php?id=124

two issues with this:
1) the site is quite popular, so a lot of people have probably bookmarked old articles and I dont want those links to die when we remove all the flat html pages.

2) I would prefer people to see human-readable urls rather than ugly querystrings.

My first instinct would be to create a big redirect file (301), sending every old article url to the new format. This addresses point one but not point two, since after the redirect the user would still see the ugly url, right? (if not, please enlighten me!)

My other thought is to use the "ugly" article.php as an include inside a wrapper, so create "article_name.php" which contains nothing but a variable for the article id and a copy of the include which takes in that variable. problems with this are a) we still clutter up our server with 1000s of files, and b) I have non-tech people messing with code and ftp, which i'd rather not.

So, what is the solution? How does mefi get those awesome clean urls like "http://ask.metafilter.com/107722/Batch-Adding-Text-File-Name-to-JPEG-Images"?

I'm guessing "107722" and "Batch-Adding-Text-File-Name-to-JPEG-Images" are some sort of aliases and not literal folders on the server. Is this something automated that is beyond the power of Wordpress? I hope not.

thanks!
posted by drjimmy11 to Computers & Internet (11 answers total) 8 users marked this as a favorite
 
You need to configure your permalinks. The .html ending trick is here.
posted by Memo at 4:15 PM on November 25, 2008


A few things. I'm not sure how much you know about this, so my apologies if I'm talking stupid-simple talk to you....

- MeFi uses Cold Fusion so what works for their URLs won't necessarily work for yours.
- the WP "permalinks" section is where the magic happens to make the query URLs into nice URLs. You change some settings there and it changes the .htaccess files to make the whole thing work. The sort of cool part about this is that the query URLs *still work* they're just not the canonical URL that the user sees.
- SO, if you can work some magical whatnot to get articlename.html to point to the query string, you can still have WP turn it into a nice URL
- I don't know how much you know about plugins, but there are some that deal with redirection in various ways. Redirection is one.

So, I'm not totally sure what you want, but as an example on my site, these three URLS

- http://www.librarian.net/stax/2520/
- http://www.librarian.net/stax/2520/world-usability-day-is-tomorrow/
- http://www.librarian.net/?p=2520

All go to the same place and the user winds up seeing the middle one [the "stax" indicator is basically saying the post is in the archive, sort of a holdover form another CMS but it works for me, it's not necessary] when they load the page. My apologies if this totally is not what you are looking for or needing to know.
posted by jessamyn at 4:15 PM on November 25, 2008


thanks, guys! I did not know about permalinks, that's great and I think I can take it from there!

One slight complication is that i want to redirect not to the actual Wordpress article page, but a page of my own making. Since the site is not a "blog" at all, I prefer to just write my own php from scratch and put "the loop" into it. I suppose if I really have to i can just paste my code into the wordpress php page in the theme folder, but I'd rather not. I much prefer to just make my own files and not dork around with Wordpress's.

So I'm not sure if that will create an issue until I actually play around with permalinks, i will come back and let you know if it's a problem!
posted by drjimmy11 at 4:22 PM on November 25, 2008


update:

looks like a combo of the permalinking stuff and using the "redirect" plugin to match old urls to new will get me what I want.

I will have to put my custom code into the WP "single.php" I think, unless I somehow make THAT redirect to my custom "article.php." But that just seems silly, so I will probably bite the bullet and bend "single.php" to my will.

thanks again!
posted by drjimmy11 at 4:43 PM on November 25, 2008


> I much prefer to just make my own files and not dork around with Wordpress's.

You can do both. WordPress's template files are purely wrapper components to contain the WP-generated output.

If your template file consists of nothing more than a series of include() commands that summon static layout components from elsewhere on your server, that's up to you; it's not a bad way to incorporate a WP blog into an existing non-WP site. But since WordPress can already know your site hierarchy (you just gave it all your site's content), you may as well allow it to generate the site menus on your behalf too. And you can also designate custom page templates per document, so one section of your site can have different skinning than another section.

The WP team has been adding a lot of features in the past half year that deepen the CMS side of things, and the coming 2.7 will add even more. So if you're going to use it at all, you should try to get everything you can out of it.

on preview: I'm slow on the trigger in adding this, but if you have questions, memail me.
posted by ardgedee at 4:44 PM on November 25, 2008


You can store extra information in the Wordpress article structure (attributes I think, from memory).

I would probably be relatively simple to use these to store the previous URL (relative to site root). Then you can create a handler that will take an otherwise undirected request, query the database for the appropriate content, and redirect from there.

If I were you I'd reconsider you decision not to use Wordpress's display code for your site. The template tools are quite robust, it is very possible to make pages that look nothing like a 'Wordpress site' while still retaining the development that's gone into the frontend handling of Wordpress.

I did quite a lot of that at my last job, unfortunately it's all been flushed from my brain, but for example these sites don't look typically Wordpressy:
bunkermedia.co.nz
starjam.org
briarwood.co.nz
posted by sycophant at 4:50 PM on November 25, 2008


If I were you I'd reconsider you decision not to use Wordpress's display code for your site. The template tools are quite robust, it is very possible to make pages that look nothing like a 'Wordpress site' while still retaining the development that's gone into the frontend handling of Wordpress.

Thanks, I am aware of this, but the site already exists and has quite a good design. I really don't need to generate menus or do anything like that. I prefer to keep all the code under my control, and just use WP as a CMS, dropping in content from the database where appropriate.
posted by drjimmy11 at 6:48 PM on November 25, 2008


(the site was quite good as it was- the reason behind the redesign was to get the content into a database, for ease of non-tech people adding new content, and to make sure the content can be easily backed up. I am quite comfortable writing html, css and php, but this is not my fulltime job and at some point I need to pass the site off to its owners.)
posted by drjimmy11 at 6:50 PM on November 25, 2008


You can store extra information in the Wordpress article structure (attributes I think, from memory).

I would probably be relatively simple to use these to store the previous URL (relative to site root). Then you can create a handler that will take an otherwise undirected request, query the database for the appropriate content, and redirect from there.


Cool idea, might be workable, except I think querying the entire database to find the post that has that url in its attributes might cause a bit of a time-lag when someone hit that link
posted by drjimmy11 at 6:53 PM on November 25, 2008


A strength of WordPress is the ability to specify your own slugs for articles. This is what you really want to do, not use numbers in a permalink string. Now, I think that slugs are limited to URLs of the format /2008/11/25/slug, which complicates things. It is a point of pride to use well-crafted slugs on my personal blog, for example.

You would inevitably end up with a mass of redirects, but that’s what redirects are for.
posted by joeclark at 6:04 AM on November 26, 2008


Cool idea, might be workable, except I think querying the entire database to find the post that has that url in its attributes might cause a bit of a time-lag when someone hit that link

SELECT post_id FROM wp_postmeta WHERE old_url = '/pages/sales-q4.html'

Then give that post id to Wordpress's link maker, and redirect to the link, or in the case of no result, treat as 404.

I still, just quietly, would recommend using Wordpress's tools. You can take your existing layout and just add the WP functions you need to populate it. Much easier than reinventing the wheels. We did this with a number of sites at my last job and in most cases the end result looked identical to the static version.

The trick is to start with the raw HTML and work backward to make it dynamic, rather than starting with a Wordpress template and trying to make it look like your site.

Among other things this means you'll be able to upgrade Wordpress without worrying about database changes completely destroying your site.
posted by sycophant at 2:55 PM on November 26, 2008


« Older They caught him in the act. Why won't he be...   |   Is planning ahead the enemy of the fresh? Newer »
This thread is closed to new comments.