Mod Rewrite Question with gallery
August 7, 2006 2:23 PM   Subscribe

I am trying to setup mod_rewrite on a custom site so that it will use mod_rewrite to make things a little cleaner. Gallery is embedded into my site so I can use my own user database.

Here is a snippet of the directory .htaccess file
-----------------------------------------------------------
RewriteEngine On
RewriteBase /

RewriteRule ^media/gallery/?(.*)$ multimedia.php?gallery&$1
RewriteRule ^media/gallery multimedia.php?gallery
RewriteRule ^login profile.php?login
------------------------------------------------------------------
The first rule is what I need help with.

The second rule works to get to the gallery but then you can't go anywhere from there to actually do stuff.

This is an example of a link that I am expecting to work with the first tag:
http://www.example.com/media/gallery/?g2_view=core.SiteAdmin&g2_return=%2Fmedia%2Fgallery23%2F%3Fg2_lery%3D%26g2_view%3Dcore.SiteAdmin%26g2_itemId%3D7%26g2_navId%3Dx16c9aa0b&g2_returnName=site+admin&g2_navId=x16c9aa0b

This should translate to:
http://www.example.com/multimedia.php?gallery&g2_view=core.SiteAdmin&g2_return=%2Fmedia%2Fgallery23%2F%3Fg2_lery%3D%26g2_view%3Dcore.SiteAdmin%26g2_itemId%3D7%26g2_navId%3Dx16c9aa0b&g2_returnName=site+admin&g2_navId=x16c9aa0b

When I manually copy and paste the second link it does work properly, so I know it is with the mod_rewrite section.

Unfortunetly I dont have access to RewriteLog (Shared Hosting)

Help me
posted by dyno04 to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
First, develop and test on a local machine. That way you can use logging to troubleshoot your rule before porting it to the live environment. If you're using OS X, Apache comes installed. If you're using Windows, there are a few easy to install packages out there, like XAMPP. If you're using another OS, you probably know how to get Apache (-:

Second, regarding your specific problem, MOD_REWRITE doesn't (as far as I know) match the contents of query strings. You can add the entire query string to your results using the $QUERY_STRING variable, so your first rule could be redone something like this (I didn't test this, but if it doesn't work I think you'll get the idea)

RewriteRule ^media/gallery/$ multimedia.php?gallery&$QUERY_STRING

There's also the 'qsappend' flag, which would allow you to "append a query string part of the substitution string to the existing string". I've never used it, but it sounds worth investigating.

Finally, the Apache docs are always good nerd readin'.
posted by alana at 3:07 PM on August 7, 2006


Best answer: I think you want

RewriteRule ^media/gallery/$ multimedia.php?gallery [R,L,QSA]

As alan said you can't match the part after the ? like you're trying to do. But [QSA] will take care of that for you. If you really want to match something in the query string you need to use %{QUERY_STRING} in the left hand side. See my comments here.
posted by Rhomboid at 3:53 PM on August 7, 2006


Oh, and get rid of the [R] if you want an internal redirect instead of an external one.
posted by Rhomboid at 3:57 PM on August 7, 2006


I just realized I if you want to use the QUERY_STRING variable on the right hand side, it needs to take the form

%{QUERY_STRING}

and not

$QUERY_STRING

as I'd used in my example.

However, using RSA would appear to be the "right" way to do this.
posted by alana at 4:32 PM on August 7, 2006


The problem with your first attempt is you're not really capturing the query string with
^whatever/?(.*)$
because the string you match this to is just the path, no domain, no query string. $1 will not contain the query. mod_rewrite only works on the path, aka "Location". you can insert the query, see below, but you can't "switch" on it

so to accomplish what everybody always wants, which is nice pretty readable urls all mapped to a single script with the pretty parts as arguments, here's my common playbook:
RewriteEngine On

#no path? straight to the script
RewriteRule   ^$                      /cgi-bin/index.pl [L]  

# some other script you want to deal with?
RewriteRule   ^/?gallery/(.*)$           /cgi-bin/gallery?page=$1 [L]  

# if the file exists in the static directory, just serve it up:
RewriteCond   %{REQUEST_FILENAME}    !-F 

# If there's a query string, pass the whole url as "url" and add the arguments ("pass through"):
RewriteCond   %{QUERY_STRING}        !^$
RewriteRule   ^(.*)$                 /cgi-bin/index.pl?url=$1&%{QUERY_STRING}     [L]

#same as above, but restated to affect this rule
RewriteCond   %{REQUEST_FILENAME}    !-F
#no query string, we do the same thing, but don't include the query string.
RewriteRule   ^(.*)$                 /cgi-bin/index.pl?url=$1     [L]
notice the [L] a the end of each RewriteCond? Means STOP, done, kaput, move along, they've met their "match".

and like the greats have said, this IS all voodoo, and it makes NO sense, and if there were anything else in the world that could possibly do anything close to this, i'd use it instead, but here we are, and i've used it a lot now... sigh, the worst part is i'm starting to "get" it

best of luck to you! you'll need it. always keep trying, even crazy shit that should never work, that's usually the solution.
posted by qbxk at 8:45 PM on August 7, 2006


Response by poster: Thanks guys, this is why ASKMEFI is the best.

I ended up doing:

RewriteRule ^media/gallery/$ multimedia.php?gallery [L,QSA]

This works exactly as I hoped.

I would have never figured out that mod_rewrite never looks at the query string.
posted by dyno04 at 10:30 PM on August 7, 2006


« Older Duff, Patrick Duffy   |   Can water go between ear and mouth? Newer »
This thread is closed to new comments.