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
$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.