Forum Moderators: phranque

Message Too Old, No Replies

dynamic subdirectory rewrite

         

headedtomexico

2:46 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



I publish a bit of php software for server operators to use. They install it in various locations on their server, and a new tool i'm adding to it requires a rewrite. I'm dynamically generating some images to be used in phpbb signature.

The bbcode filter wont parse links that dont end with png/jpg/gif/etc. This stops me from passing GET parameters, and having a .php ending image.

The solution is to use a rewrite and pass the parameters as subdirectories, and i've figured that out, but i'm having problems making it dynamic since installation location will vary.

I want the .htacces to be in my softwares directory, not their root directory, and I want to avoid them having to alter it.

for example, the dynamic image is saved at installdirectory/phpbbsignature.php and needs a ?name=bob to gen bobs signature.

Currently i'm using


RewriteRule ^phpbbsig/([A-Za-z0-9/-]+)/mysig.png$ /phpbbsignature.php?name=$1 [L]

This works on my test site as i have a subdomain that it is installed in, so when i visit the subdomain...
[subdomain.mysite.com...]
it changes to
[subdomain.mysite.com...]

The problem is that I have no idea where the server admins installing my stuff will be putting it and mod_rewrite seems to strip leading directory names off when .htaccess is in a folder. For example if I visit the same exact page by typing out the folder name the subdirectory is mapped to (i.e. sobdomain.mysite.com points to mysite.com/subdomain) I get the following.
[mysite.com...]
it changes to
[mysite.com...]

it strips out the subfolder name...

I understand I can fix this by putting .htaccess in the root directory, but not all my users are very savvy, and if there is already one there I would have to be offering extra support in helpng them merge the two files. If I leave it in the install directory then I would have to be offering them support still to modify the file to add on the subfolder names....

Is there an alternative to add the stripped subfolder names back on, and it be dynamic even if theyinstall it in [newsite.com...]

I've really been hunting around and trying alot of stuff. I've tried several things unsuccesfully... if anyone has a solution I would really appreciate it.

jdMorgan

4:50 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using a RewriteCond to examine and back-reference the URL-path in %{THE_REQUEST}.

Jim

headedtomexico

5:57 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



okie-dokie =) if i'm succesfull i'll post the good code, if I fail, i'll be back

headedtomexico

8:13 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



ok.. i'm a little lost with this stuff, the reference material out there is a little vague and can't find many examples of stuff with the_request. Here is what I got, and i'm kinda assuming its wrong


RewriteCond %{THE_REQUEST} ^GET\ ([.+])/phpbbsig/[.+]$
RewriteRule ^phpbbsig/([A-Za-z0-9/-]+)/mysig.png$ %1/phpbbsignature.php?name=$1 [L]

to illustrate what I would expect/want this to do here are some examples in the order of:
.htaccess location
requested url
rewrite url

#1
[mysite.php...]
[mysite.php...]
[mysite.php...]

#2
[subdomain.mysite.php...]
[subdomain.mysite.php...]
[subdomain.mysite.php...]

basically htaccess will be in whatever folder phpbbsignature.php is in, and its parent folder could be ANYWHERE since this released software

Did i even get close?

jdMorgan

10:50 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Kind of rushing here, but your regex patterns look wrong. Try:

RewriteCond %{THE_REQUEST} ^GET\ /([^#?\ ]+)/phpbbsig/
RewriteRule ^phpbbsig/([A-Za-z0-9/\-]+)/mysig\.png$ /%1/phpbbsignature.php?name=$1 [L]

Jim

headedtomexico

11:28 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



cool thanks, i was acutally playing with it in the same window and got the following working


RewriteCond %{THE_REQUEST} GET\ (.+)phpbbsig/

I like the range from yours, i've heard its faster that way, but for some reason it had some problems with the leading /, i guess it starts with "http". I dunno... anyway here is the final product for anyone else that stumbles by this thread. Thanks very much for the help jim.


RewriteCond %{THE_REQUEST} ^GET\ ([^#?\ ]+)phpbbsig/
RewriteRule ^phpbbsig/([A-Za-z0-9/\-]+)/mysig\.png$ %1/phpbbsignature.php?name=$1 [L]

jdMorgan

1:33 am on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If "it had a problem with the leading slash," then you need to get to the bottom of that problem, because you *really* do not want anyone but you to be able to determine the 'start' of the filepath on your server -- It's a security problem.

So, if possible, please try to figure exactly why there is a problem -- You really need to start the substitution path with a slash, rather that with "whatever the client sends" as I did in the example I posted.

By the way, THE_REQUEST is the HTTP request line as received from the client -- and exactly as it appears (usually) in your raw server access log file. e.g

GET /abc/def/phpbbsig/ HTTP/1.1

Jim