TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-05-2008, 05:36 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default Escaping Metacharacters

I need to escape the '?' in RegEXP

Code:
RewriteRule ^image(?=this is where I want my '?')([A-Za-z0-9]+)?$ image.php?img=$1 [L]
I've tried many ways, none worked.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 06:10 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Using a backslash doesn't work? I escape periods in my rewrite's just like so \. with no problems. What about using QSA instead if it doesn't work? If I remember correctly you should be able to do

RewriteRule ^image$ image.php [QSA, L]

and it should work like /image?img=image.png => /image.php?img=image.png. Worst possible scenario you could do

RewriteRule ^image/([A-Z0-9]+)/?$ image.php?img=$1 [NC, L]

and site.com/image/image.png would pump it through your php file. According to this
though you should be able to escape with \... but I tried it and it wasn't working either, so I'm lost.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-05-2008, 06:18 AM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Special escaping of quantifiers in mod_rewrite?

This may be some help too. You may have to use RewriteCond instead of just RewriteRule.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-05-2008, 06:18 AM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Using a backslash doesn't work? I escape periods in my rewrite's just like so \. with no problems. What about using QSA instead if it doesn't work? If I remember correctly you should be able to do

RewriteRule ^image$ image.php [QSA, L]

and it should work like /image?img=image.png => /image.php?img=image.png. Worst possible scenario you could do

RewriteRule ^image/([A-Z0-9]+)/?$ image.php?img=$1 [NC, L]

and site.com/image/image.png would pump it through your php file. According to this
though you should be able to escape with \... but I tried it and it wasn't working either, so I'm lost.
-m
QSA gives me the 500 error, and NC does aswell.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 07:20 AM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Sorry, make sure there's no spaces. [QSA,L] or [NC,L]. QSA is for your query string, NC is just non case sensitive.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-05-2008, 07:24 AM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Sorry, make sure there's no spaces. [QSA,L] or [NC,L]. QSA is for your query string, NC is just non case sensitive.
-m
Neither work, this is the same problem I get:
Code:
Not Found

The requested URL /image was not found on this server.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:33 AM   #7 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

How does your whole .htaccess or .conf file looks like? Can you post it here?
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:34 AM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
How does your whole .htaccess or .conf file looks like? Can you post it here?
nothing much actually, just RewriteEngine on, with a few RewriteRule's, and that one. so yeah
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:37 AM   #9 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

To clear things up what do you want to do exactly? It seems you want to rewrite /image/bla.jpg to /image.php?img=bla.jpg am I right?
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:38 AM   #10 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
To clear things up what do you want to do exactly? It seems you want to rewrite /image/bla.jpg to /image.php?img=bla.jpg am I right?
no image?bla.jpg
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:42 AM   #11 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

This works on my computer
Code:
RewriteRule ^image([A-Za-z0-9\.\_]+)?$ image.php?img=$1 [QSA,L]
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:43 AM   #12 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
This works on my computer
Code:
RewriteRule ^image([A-Za-z0-9\.\_]+)?$ image.php?img=$1 [QSA,L]
img=$1 doesnt work, I have it where it echos, for quick testing. so yeah, doesnt work. :[ and I have mod_rewrite enabled

I want it like image?bla.jpg, not imagebla.jpg
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:48 AM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
That's pretty strange

Just works

can you put print_r($_GET)); in your image.php file and post what happens..
o0okay
fsss
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:50 AM   #14 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

I used var_dump instead of print_r, cause its more effective, and I got this:
PHP Code:
string(0"" 
:S

Update: I removed QSA, now it just gives me .php
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:50 AM   #15 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

But why don't you just use image/bla.jpg this looks much cleaner..
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:55 AM   #16 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
But why don't you just use image/bla.jpg this looks much cleaner..
Sigh.. So let me guess, you can't find the answer?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:58 AM   #17 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

No it seems apache doesn't allow ?'s in their regexes, even if you pass it in as a hex it doesn't work.. and when you use / it's pretty basic..
Code:
RewriteRule ^image/([A-Za-z0-9\.\_]+)?$ image.php?img=$1 [QSA,L]
sjaq is offline  
Reply With Quote
Old 05-05-2008, 10:00 AM   #18 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
No it seems apache doesn't allow ?'s in their regexes, even if you pass it in as a hex it doesn't work.. and when you use / it's pretty basic..
Code:
RewriteRule ^image/([A-Za-z0-9\.\_]+)?$ image.php?img=$1 [QSA,L]
Well then, thats ass.. :P they need to update that. at least make it like this \? or [\?] or whatever
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 10:05 AM   #19 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Ah I found it, when you use %3F it works, that is the hex for a ?. So that was the problem. It is just a technical restriction.. But you could fix it with php...
Code:
RewriteRule ^image$ image.php [QSA,L]
PHP Code:
// get all array keys
$gets array_keys($_GET);
// check if it is a valid image (you may want to check if it exists
$img preg_match('/[a-zA-Z0-9\.\_]+/'$gets[0])? $gets[0] : null;
// because it is a key apache replaces .'s with _'s we need to fix that
$xp explode('_'$img);
$count count($xp)-1;
$ext $xp[$count];
unset(
$xp[$count]);
// the image
$img implode('_'$xp) . '.' $ext
sjaq is offline  
Reply With Quote
The Following User Says Thank You to sjaq For This Useful Post:
Orc (05-05-2008)
Old 05-05-2008, 03:28 PM   #20 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I tried the following which seemed to work ok on my system:

RewriteCond %{QUERY_STRING} ^[a-z0-9]{1,20}\.(?:jpe?g|gif|png|bmp)$ [NC]
RewriteRule ^image$ img.php?img=%0 [L]


One thing to note is that your PHP file shouldn't be named the same as what comes before the query string, ie image?test.jpg should not use image.php because it appears that Apache's content negotiation gets in the way and serves up the PHP page without going through the rewrite process. Simply rename the PHP page to something else as I have in my example above, img.php
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design