MJC SEO & Web Development Blog > Gallery 3 SEO – Part Two – Improved Photo URLs

Gallery 3 SEO – Part Two – Improved Photo URLs

Batch Altering Photo URL slugs in Gallery 3 with a PHP Script

This is part two of a series on Gallery 3 SEO. Read Part one at Gallery 3 SEO – Part One – The Perfect URL Scheme

Gallery 3 creates URL slugs for photos on import based on their filename. If your setup is anything like mine, this will result in photos with URLs such as /gallery/album/DSCF3043 – which is not great.

The Captionator Module can be used to give Titles and Captions to photos of an entire album from one screen, similar to the old Gallery 2 system. However, this doesn’t allow setting of the URL path of the photo, the only way to do this is by going through the album and clicking “edit photo” on each photo page.

One solution would be to extend the Captionator module to allow setting of the URL slug too, but a more elegant solution is to create URLs based on the photo’s titles with a batch script.

N.B. The script in this guide will change URLs of photos but not create a corresponding redirect – if used on an established gallery, a map of the old URLs should be created beforehand and a list of 301 redirects to the new URLs written afterwards to prevent broken external links.

Instructions:

  1. Give all the photos in the album a unique title. As previously mentioned, the Captionator Module is good for this.

  2. The script is written to carry out this operation to just one album at a time for safety. This is best done using the unique ID of the album.

    Load phpMyAdmin or equivalent database browser, and navigate to the items table. Look through the table (it may be useful to set the display to 999 rows per page) and find the parent album.
    Look up its item_id and make a note of this.

    Or, if your album name is definitely unique, you can skip this step and use the alternate method allowed in the script. But this is risky, because if say your album’s URL slug is “england” and you use this, if you had an album with a url slug of “new-england” these rows would also be selected by the query, as would a photo with url slug “steve-england-portrait”.

  3. Create an emtpy PHP file on your server and paste in the following code:

  4. <?php
    /* UPDATE DB RECORDS FOR GALLERY 3 PHOTO URLS
    Author: Miles Carter http://www.milesjcarter.co.uk/blog/ */
       

    // EDIT YOUR GALLERY 3 DATABASE DETAILS
           
    $db_gallery = mysqli_connect('localhost', 'DBNAME', 'PASSWORD', 'USER');

    /* Select Rows based on Parent Album ID (safe)
     Replace NUMBER with the Item ID of parent gallery */


    $query = 'SELECT * FROM items WHERE mime_type="image/jpeg" AND parent_id=NUMBER';

    /* Select Rows based on unique fragment of URL to identify album (more risky)
    Example use: uncomment following line and replace "%URLSLUG%" with "%australia%" */


    //$query = 'SELECT * FROM items WHERE mime_type="image/jpeg" AND relative_url_cache LIKE "%URLSLUG%"'

    $results = mysqli_query($db_gallery, $query);
    while ($row = mysqli_fetch_assoc($results)) {
    $theid = $row['id'];

    /* Process photo title into correct format for slug
     e.g. "Dave's House is Big!" to "daves-house-is-big" */


    $string = preg_replace('/[^A-Za-z0-9-_\s]+/', '', $row['title']);
    $string = strtolower(urlencode(preg_replace('/\s|_/', '-', $string)));
    $string = preg_replace('/-+/', '-', $string);
    $string = preg_replace('/-$|^-/', '', $string);

    // Update DB
    $query_insert = "UPDATE items SET slug='$string' WHERE id='$theid'";
                                   
    if (!mysqli_query($db_gallery, $query_insert)) {

    echo '<p>Error doing script: ' .
             mysqli_error($db_gallery) . '</p>';
    }
    else { echo "record change attempted $theid $string <br>"; }
    }

    mysqli_close($db_gallery);
    ?>
  5. Update this with your database details, and the item_id of the parent album. Save the file, then navigate to it by typing the URL into a web browser – e.g. http://www.example.com/gallery/db-photo-url-script.php

  6. You should see output like this:

    record change attempted 821 sydney-in-autumn
    record change attempted 820 sydney-icons
    record change attempted 809 paperweight-rock
    record change attempted 818 deserted-manly-beach
    record change attempted 819 parramatta-river-ferry
    record change attempted 814 ahead-of-the-storm
    record change attempted 815 anvil-cloud
    record change attempted 811 grand-canyon
    record change attempted 813 grose-valley-walls
    record change attempted 803 supermarket-offroader
    record change attempted 800 bush-track
    record change attempted 806 banded-rock
    record change attempted 802 wollemi-campfire
    record change attempted 807 honeycomb-rock
    record change attempted 808 table-rocks
    record change attempted 805 firestick
    record change attempted 812 above-govetts-creek
    record change attempted 799 wollemi-camping
    record change attempted 816 edge
    record change attempted 817 teepee
    record change attempted 804 st-georges-basin-sunset
    record change attempted 795 rock-swirls
    record change attempted 792 hardware-store-katoomba
    record change attempted 793 street-wildlife
    record change attempted 794 the-cliffs-edge
    record change attempted 822 spiral-park
    record change attempted 796 cliff-top-woodland
    record change attempted 797 blue-mountains
    record change attempted 798 ghost-gum-tree

    But at this point, the URLs are only changed in the database, not on the site.

  7. Go to the Maintenance Section of admin, and chose the “fix a variety of problems” option. This will rebuild caches (along with unknown other things…) – after this, the newly set URLs will be working.

You will now have a good URL scheme for all the photo pages in your Gallery 3 album.

Part one of the Gallery 3 SEO scheme series – The Perfect URL Scheme

5 Comments

  1. Posted December 15, 2010 at 12:34 am | Permalink

    Thanks for the wonderful post!

  2. Posted August 20, 2011 at 2:44 pm | Permalink

    Thanks for two very useful parts for SEO my gallery websites.

    Is there any instruction for creating meta description and meta keywords for my gallery3 websites?

  3. Ducky
    Posted December 12, 2011 at 11:36 am | Permalink

    Is there a way to create a map of the current not sef urls in gallery 3, before implementing the captionator PHP script?

  4. Miles Carter
    Posted April 11, 2012 at 5:08 am | Permalink

    Hi, Apologies for not seeing or approving your comments sooner, I had a serious issue with spam comments on this blog, which is now fixed.

    Ricky: There’s this module http://codex.gallery2.org/Gallery3:Modules:custom_header

    However I think it would take extra coding to get it to do more than just output the same meta description for every page – which would not be good SEO.

    Ducky: You can user a link spider program, I use Xenu’s Link Sleuth because it’s freeware and does the job. http://home.snafu.de/tilman/xenulink.html – It will generate a site map for you after it’s finished spidering. Or you can export the results to a spreadsheet program for more detailed analysis.

  5. Posted November 30, 2012 at 1:28 pm | Permalink

    Hello Miles, I am working on Part one of the Gallery SEO-tips – I like the post and I think it will do the URL structure much good.

    Regarding this part, if I am correct, the Caption module currently does allow for changing the URL (as well as filename and tags) for all both albums and photos. Saves time and customization!

Post a Comment

Your email is never shared. Required fields are marked *

*
*