Jump to content

MOD_REWRITE being fiddly


deadonarrival

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/106502-mod_rewrite-being-fiddly/
Share on other sites

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

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.

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)

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.