deadonarrival Posted May 20, 2008 Share Posted May 20, 2008 I'm finding MOD_REWRITE to be a bit of a pain, so I'll just fire out a couple of questions 1) Is it possible to/how can I detect if MOD_REWRITE is enabled or not via PHP. 2) Is it possible to make PHP set .htaccess values (namely RewriteBase) 3) realpath(); and mod_rewrite - will the extra "folders" confuse the realpath function? Not quite related but 4) Is there any way to find the directory one above the document root - I want to save my sqllite database one level above, without knowing how many levels down the app is. Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/ Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 1. i don't think theirs a feature/function, but you could use a trick, like always pass a get called rewrite, then use isset($_GET['rewrite']) = mod_rewrite is running. 2. Yes & No, Yes it can create the file BUT .htaccess file load before the PHP is parsed, so you could write an admin tool to make changes but not really create a dynamic .htaccess file, (which shouldn't be needed) 3. Not that i have found. 4. Yes, realpath($_SERVER["DOCUMENT_ROOT"]."/.."); should do it Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-545945 Share on other sites More sharing options...
deadonarrival Posted May 20, 2008 Author Share Posted May 20, 2008 Thanks for the reply. I've gotten home now, so I'm able to actually try some of this instead of just thinking about it, and I've hit on a problem so to speak. I've got this .htaccess <IfModule mod_rewrite.c> RewriteEngine on # RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] </IfModule> Obviously the r variable contains almost everything. I want to try to extract the code, but keep key=>value pairs seperate, so I've managed to get this code. $path = explode("/",$_GET['r']); foreach($path as $key=>$val) { $split = explode(".",$val); $arg[$split['0']] = $split['1']; } Which outputs $path = Array ( [0] => modulename [1] => 1.val1 [2] => two.v2 [3] => (blank) ) $arg = Array ( [modulename] => [1] => val1 [two] => v2 [] => ) Two obvious problems here are that I can't just extract the module from the arg array, and that the 4th value (id 3) is superflous. I can see that the second problem arises from my first explode (it goes if I take out the trailing slash). But is there any way to sort the first problem? I thought of just using $module = path[0] and then doing the rest as arguments, but that leaves my code a little messy. Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-546095 Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 It maybe because its late and i have had little sleep for the last 15 days but.. i'm confused:~ can you give an example of the url and what your trying to do with it Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-546121 Share on other sites More sharing options...
deadonarrival Posted May 20, 2008 Author Share Posted May 20, 2008 Right, my root is www.site.com/path/to/application/ But it could also be www.site.com/ or www.site.com/path/n/n+1/... .../end/of/the/world/ Basically - it could be the root on the site, or could be 5000 levels in. The url itself (assuming we're in the root www.site.com) I want to be www.site.com/modulename/key/val/key/val/ so we might end up with www.site.com/blog/id/4/reply/5/add/ but I was aiming for www.site.com/blog/id.4/reply.5/add/ I'm trying to get /blog/ as a variable, and any key.value pair as $key = value (or $arg['key'] = value). As a last twist, I'm trying to make any thing with no period into a $key = true or $arg['key'] = true; I've played with my regexp and got this: ^(.+)/(((.*)\.(.*))*)$ Which should allow /(any string of 1 or more chars)/(any string).(any string)/ Where the (any string).(any string) can be 0 or more repetitions. I think my regexp will work, but I'm not sure how to implement it, or if there's a better way to do it. (Admins - this might be better moved into regexp, the topic's changed a little) Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-546124 Share on other sites More sharing options...
deadonarrival Posted May 20, 2008 Author Share Posted May 20, 2008 Additionally: I've got the following working regexp ^([a-zA-Z0-9_-]+(/[a-zA-Z0-9]+(.[a-zA-Z0-9]*?)*/?)?$ 0 or 1 of the following (a string) / (a string).(optional string) optional / But now it crashes (server error) Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-546142 Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 i think this regex would suite better ^([^/]+)/((??:(?:[^\.]+\.)?[^/]+)/?){0,})/(.*?)/+$ then use RewriteRule ^(.*)$ index.php?module=$1&keys=$2&action=$3 [L,QSA] from blog/id.4/reply.5/add/ $1 = blog $2 = id.4/reply.5 $3 = add or blog/id.4/reply5/add/ $1 = blog $2 = id.4/reply5 $3 = add try this.. <?php echo $_GET['module']; $keys = $_GET['keys']; $path = explode("/",$keys); foreach($path as $key=>$val) { $split = explode(".",$val); $arg[$split['0']] = (isset($split['1']))?$split['1']:true; } echo $_GET['module']; echo $_GET['action']; ?> anyways i am getting some sleep Quote Link to comment https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/#findComment-546146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.