Meta

  1. Recent blog posts

  1. Papers and HowTos

Recent output

  1. Gallery2, URL Rewriting, and (Sun|Open) Web Server

  2. Thu, 07/19/2007 - 18:52 — jmccabe

    [Note: I no longer work for Sun. I am slowly migrating the more worthwhile blog entries I made for Sun to my own blog. I need the Google love.]

    Originally posted on July 19, 2007:

    On my personal web site I use Sun Java System Web Server 7.0 and a slew of PHP applications. One of them, Gallery2, has built in support for URL rewriting. Unfortunately it depends on an extension to Apache called mod_rewrite and the syntax for the same functionality in Web Server is a bit different. Because of this I've been putting off enabling the feature until I could spare a few hours to research the differences and rewrite the rules. Here's what I came up with.

    I've enabled the Gallery2 URL Rewrite module with these options:

    • Show Item
    • Download Item
    • Add Comment
    • View Comments
    • Slideshow
    • RSS
    • Simple RSS Feed
    • Updates Album
    • Popular Album
    • Random Album

    My Gallery2 has its own vanity domain, so it's deployed as the root of the URI space.

    Gallery2 helpfully generated an htaccess file with these rules (I've annotated for my own understanding - Please point out errors I've made):

        RewriteBase /
    
    # Stop processing if the requested file actually exists, or the request is
    # for main.php or gallery_remote2.php
    # I'm opting to skip this in my translation. It seems a bit unneeded to me.
        RewriteCond  -f [OR]
        RewriteCond  -d [OR]
        RewriteCond  gallery\_remote2\.php
        RewriteCond  !/main\.php$
        RewriteRule .   -   [L]
    
    # If the request is for /popular, and the URI is _not_ for /main.php, grab
    # Restart the request with the URI changes to the canonical album syntax, and
    # append the query string to the end of the new URL (used for paging to pages past
    # the front page of the album).
    # I notice that all the rules specifically call out main.php in their check. This seems
    # overkill to me, so I'll just check once at the beginning of my translation
        RewriteCond  /popular(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=dynamicalbum.PopularAlbum   [QSA,L]
    
    # Same, but for Updates
        RewriteCond  /updates(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=dynamicalbum.UpdatesAlbum   [QSA,L]
    
    # Same, but for Random
        RewriteCond  /random(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=dynamicalbum.RandomAlbum   [QSA,L]
    
    # URI space for downloading items (e.g. "Save Image")
        RewriteCond  /d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3   [QSA,L]
    
    # More of the same, but for the slideshow
        RewriteCond  /v/([^?]+)/slideshow\.html(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=slideshow.Slideshow&g2_path=%1   [QSA,L]
    
    # Comment viewing
        RewriteCond  /c/view/([0-9]+)\.html(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=comment.ShowAllComments&g2_itemId=%1   [QSA,L]
    
    # Comment adding
        RewriteCond  /c/add/([0-9]+)\.html(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=comment.AddComment&g2_itemId=%1   [QSA,L]
    
    # RSS and Simple RSS rules
        RewriteCond  /rss/([^\/\?]+)(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=rss.Render&g2_name=%1   [QSA,L]
        RewriteCond  /srss/([0-9]+)(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_view=rss.SimpleRender&g2_itemId=%1   [QSA,L]
    
    # The main URL cruft for viewing most items. I should probably make this my first
    # "Real" rule
        RewriteCond  /v/([^?]+)(\?.|\ .)
        RewriteCond  !/main\.php$
        RewriteRule .   /main.php?g2_path=%1   [QSA,L]

    My translated rewrite rules for Web Server 7 are:

    # At the top of my default block
    # Only enter this stanza if this request is for my vanity VS for photos, and the URI isn't
    # for /main.php
    <If $urlhost = "photos.foo.com"
         and $uri !~ '/main\.php'>
    
        # If the request is for /popular
        <If $url =~ '/popular(\?(.*)|())'>
          # Restart the request with the canonical URI (and query string, represented here
          # by "&$2" - Watch for it below too, but notice that the backreference ID changes
          # based on where the parens are in the expression)
          AuthTrans fn="restart" uri="/main.php?g2_view=dynamicalbum.PopularAlbum&$2"
        </If>
    
        # If the request is for /updates
        <ElseIf $url =~ '/updates(\?(.*)|())'>
          # Restart with canonical URI and query string
          AuthTrans fn="restart" uri="/main.php?g2_view=dynamicalbum.UpdatesAlbum&$2"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/random(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=dynamicalbum.RandomAlbum&$2"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/d/([0-9]+)-([0-9]+)/([^/?]+)(\?(.*)|())'>
          # Restart with the canonical URI and query string, but notice that the backreference
          # for the query match is now $5 rather than $2 above
          AuthTrans fn="restart" uri="/main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&g2_fileName=$3&$5"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/v/([^?]+)/slideshow\.html(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=slideshow.Slideshow&g2_path=$1&$3"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/c/view/([0-9]+)\.html(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=comment.ShowAllComments&g2_itemId=$1&$3"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/c/add/([0-9]+)\.html(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=comment.AddComment&g2_itemId=$1&$3"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/rss/([^\/\?]+)(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=rss.Render&g2_name=$1&$3"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/srss/([0-9]+)(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_view=rss.SimpleRender&g2_name=$1&$3"
        </ElseIf>
    
        # etc
        <ElseIf $url =~ '/v/([^?]+)(\?(.*)|())'>
          AuthTrans fn="restart" uri="/main.php?g2_path=$1&$3"
        </ElseIf>
    </If>

    I'm pretty happy with the results so far.

    Also remember that this set of rules only restarts requests coming into the server; it does nothing to make Gallery2 write the URLs in a shorter, friendlier way. You still have to turn on the Gallery2 URL Rewrite module, etc. This tickles the Gallery2 code that generates the links. One could also use a Web Server output filter to rewrite the HTML from Gallery2 (or any other app) before it hits the wire, but it's better to have the app do it itself.

     

Small Things

  1. Monthly archive