Skip navigation.

Drupal

SQL Query for mapping Drupal Node Titles to URL Aliases

I'm not sure how often you need to do this, but if you've ever wanted to build a cheat sheet that matches Drupal page titles to each page's clean url then here's the SQL query that'll do the trick:

select n.title as Title, u.dst as URL, u.pid as ID from (select title, concat('node/', nid) as realurl from node) as n join url_alias u on n.realurl = u.src order by n.title, u.pid desc

To avoid broken links Drupal tends to hang on to old aliases.  So for better or worse you can end up with duplicate URLs for the same page and title.  For that reason I've included the index id for the aliaes in the query and then used them as a secondary sort.  At least in Drupal 6 and below the highest Alias ID corresponds to the most recently created alias.  Which is most likely to be the "correct" one.  Oh yeah, and I've included the index id for pages as well because in Drupal it's possible for two pages to have the same title.

The result's going to look something like this

 

Page IDTitleURLAlias ID
2312009 - 2010 Wholesale Price List2009-2010-wholesale-price-list897
2312009 - 2010 Wholesale Price List2009-2010_pricelist_wholesale837
2312009 - 2010 Wholesale Price Listnursery/wholesale_price_list836
2312009 - 2010 Wholesale Price Listpricelist835
242Availability Updateblog/2010-11/availability-update874
246Availbility Sheetnodeavailbility-sheetnode900
202Before and After Photosand-after-photos878
202Before and After Photosbefore_after755
202Before and After Photosbefore_after754
202Before and After Photosbeforeandafters753
206Catalogcatalog767
206Catalogcatalog766

In case you're curious I use lists like this as a link-mapping tool when I import an old website's pages into Drupal.  Once the pages are imported, the titles straigthened out, and the URLs mapped into a decent hierarchy I can use this list when I go through the actual content (remember content?) and replace old urls with new correct ones.

There's probably a more intelligent...or at least less complicated way to do it, but this works for me.  For a small site you wouldn't bother, but if you're importing hundreds or thousands of pages it's... kind of helpful.

Syndicate content