Rewriting the WPG2 URL

I’m sick and tired of the new WPG2 plugin for WordPress forcing me to use the embedded gallery page. Sure, nowadays you can configure it to look identical to the actual gallery, but that doesn’t satisfy me. Since my gallery is on another virtual domain (gallery.fredrikbostrom.net as opposed to blog.fredrikbostrom.net) I want the images in my WordPress sidebar to link to a page where the URL tells me that I’ve moved away from the blog and am now viewing the gallery. WPG2 version 3.x doesn’t do this, so I decided to fix it myself (yet again a case of “if you want something done, do it yourself).

The solution is as simple as it’s elegant. I first tried adding a RewriteRule directive to the virtual domain configuration file of my blog (/etc/apache2/sites-enabled/blog or similar). This wasn’t good since it was being overridden in a local .htaccess file in the wordpress web directory (/var/www/wordpress). Also, I decided to use the RedirectMatch directive instead as it seemed a bit simpler.

Now, the elegance in this is that the WPG2 embedded page takes the same arguments as the actual gallery, so replacing the URL up to the parameters with the correct address simply solves the problem. Specifically, I put this directive as the first line in the file /var/www/wordpress/.htaccess:

RedirectMatch ^/wpg2(.*) http://gallery.fredrikbostrom.net/main.php$1

The first part (^/wpg2(.*)) captures the WPG2 embedded URL, i.e. http://blog.fredrikbostrom.net/wpg2) and “remembers” anything after /wpg2, which for example could be ?g2_itemId=17711. The $1 in the latter part is then substituted with this memorized string, yielding a perfectly valid gallery2 URL.

As it turned out, the ^ character is quite important here, since it only matches /wpg2 when it appears directly after the host name. Without it, any URL containing the character sequence /wpg2 (like this blog post) would redirect to the gallery.

Furthermore, if the embedded URL doesn’t receive any parameters, the resulting URL is still valid and points to the gallery’s front page, which the embedded URL also would.

I can’t understand why a simple thing like this can be so hard for the WPG2 team to include.

Advertisements

2 Comments

Filed under All software sucks, Geek speak, Linux, Photography

2 responses to “Rewriting the WPG2 URL

  1. Chris

    Hey Fred,

    You seem to really get a hang of this. If you don’t mind me asking I got a wpg problem too. When I use the album jump menu it takes me to:

    mysite/g2/main.php?g2_itemId=116

    but I really want it to jump to
    mysite/?g2_itemId=116

    Can this be rewriten in htaccess?

    Cheers

  2. Chris,

    sure you can. Try with

    RedirectMatch ^/g2/main.php(.*) mysite/$1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s