Using ModRewrite (4 posts)

  1. samwaudby
    Posted 11 years ago #

    Hi all,

    Wondered if someone could help me writing a custom mod rewrite rule for a gallery page I use.

    The format is

    I would like for it to be:


    I've looked through the codex, but all of it seems to be to do with changing post and page names not the broad subject of modrewrite.


  2. Mark (podz)
    Support Maven
    Posted 11 years ago #

    There are few mod_rewrite specialists here, very few.
    Have you tried #apache on irc.freenode.net ? They many be able to point you to some good sources as it's their area of interest.

  3. rwatkins
    Posted 11 years ago #


    The Apache Docs would be better than the codex for this.

    There will be loads of examples on the net too, I reckon. Just google for RewriteRule or mod_rewrite.


  4. rwatkins
    Posted 11 years ago #


    Just had a bit more of a look at this. To do something like the second of your requests, you'd need a .htaccess inside the "album" directory containing something like this (it might not be totally correct but it works ;)).

    RewriteEngine On
    RewriteBase /album
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?pp-album=$1&pp_cat=$2

    This would redirect anything in the form of http://www.yourdomain.tld/album/ducks/geese/ to /album/index.php?pp-album=ducks&pp_cat=geese.

    Note that the [^/] (read: anything but a /) are there as it will match them otherwise - and you probably want /album/ducks/geese/ganders/ to give an error rather than try and redirect to pp-album=ducks/geese&pp_cat=ganders.

    You are going to have to know bits of regex to do this, but it's not *that* tricky.

    So, to do the "picture.jpg" you would want a RewriteRule like:

    RewriteRule ^([^/]+)/([^/]+\.jpg)$ index.php?pp-album=$1&picture=$2 [L]

    this as the first rule. (the [L] stops the next rule being processed (which would mess things up a tad if it was processed))

    Obviously you want different types of filenames and so it's probably better to forward anything with a "." in last part of the path to the album script as an image (rather than a category):

    RewriteRule ^([^/]+)/([^/]+\.[^/]+)$ index.php?pp-album=$1&picture=$2 [L]

    You can probably work out the album bit too.

    None of this is particularly beautiful (nor is it totally correct) -- I tend to just hack around with stuff like that until it works. It might be a start for you to have a go exploring what you can do, however.


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.