mod_rewrite and relative paths help

This is a discussion on mod_rewrite and relative paths help within the Apache Web Server forums, part of the Web Server and Related Forums category; hi group. i've been using mod_rewrite for simple things so far and always encountered the same (basic) problem, which ...


Go Back   Usenet Forums > Web Server and Related Forums > Apache Web Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-19-2007
Susanne West
 
Posts: n/a
Default mod_rewrite and relative paths help



hi group.


i've been using mod_rewrite for simple things so
far and always encountered the same (basic) problem,
which is, that relative paths get messed up.

i was wondering if i'm doing something conceptionally wrong
if i use the following setup:



i have multiple (empty!) directories on my disk that i'd like
to use as 'virtual' urls (e.g.):

/about
/products
/detail

as well as a special directory with a handler-script
/frontend/handler.php
that should be targeted by .htaccess-files in the directories
above. so each directory listed contains a .htaccess file with
the following (accordingly adapted) lines:

RewriteEngine On
RewriteBase /about/
RewriteRule .* /frontend/handler.php?mode=about
RewriteRule ^index.html$ /frontend/handler.php?mode=about

is that a correct way to handle the overall situation?
because the problem arises, when i'd like to additionally map an
exception from another level of the path hierarchy, like mapping the
actual homepage [/index.html] down to /frontend/handler.php?mode=home
because obviously, then, the relative links to images, stylesheets etc
are different and the page looks totally broken...

how are you guys handling this?


greets!



  #2 (permalink)  
Old 02-19-2007
shimmyshack
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

On 19 Feb, 11:39, Susanne West <s...@gmx.de> wrote:
> hi group.
>
> i've been using mod_rewrite for simple things so
> far and always encountered the same (basic) problem,
> which is, that relative paths get messed up.
>
> i was wondering if i'm doing something conceptionally wrong
> if i use the following setup:
>
> i have multiple (empty!) directories on my disk that i'd like
> to use as 'virtual' urls (e.g.):
>
> /about
> /products
> /detail
>
> as well as a special directory with a handler-script
> /frontend/handler.php
> that should be targeted by .htaccess-files in the directories
> above. so each directory listed contains a .htaccess file with
> the following (accordingly adapted) lines:
>
> RewriteEngine On
> RewriteBase /about/
> RewriteRule .* /frontend/handler.php?mode=about
> RewriteRule ^index.html$ /frontend/handler.php?mode=about
>
> is that a correct way to handle the overall situation?
> because the problem arises, when i'd like to additionally map an
> exception from another level of the path hierarchy, like mapping the
> actual homepage [/index.html] down to /frontend/handler.php?mode=home
> because obviously, then, the relative links to images, stylesheets etc
> are different and the page looks totally broken...
>
> how are you guys handling this?
>
> greets!


If you just used
ReWriteBase /
ReWriteCond !-f
ReWriteCond !-d
RewriteRule . /frontend/handler.php

and then used the value of $_SERVER['REQUEST_URI']
you would be able to get all the info you needed, pasrsing the url
will get you all the info into an array and then you can make the
paths that you need to send and prepend them.

My advice is to steer clear of relative urls in your html. What's the
use of context driven resources (ie, ../../../../images/static/2.jpg,
when you could just have /images/static/2.jpg - this way when you move
your css directory you don't have a headache.



  #3 (permalink)  
Old 02-19-2007
shimmyshack
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

On 19 Feb, 11:39, Susanne West <s...@gmx.de> wrote:
> hi group.
>
> i've been using mod_rewrite for simple things so
> far and always encountered the same (basic) problem,
> which is, that relative paths get messed up.
>
> i was wondering if i'm doing something conceptionally wrong
> if i use the following setup:
>
> i have multiple (empty!) directories on my disk that i'd like
> to use as 'virtual' urls (e.g.):
>
> /about
> /products
> /detail
>
> as well as a special directory with a handler-script
> /frontend/handler.php
> that should be targeted by .htaccess-files in the directories
> above. so each directory listed contains a .htaccess file with
> the following (accordingly adapted) lines:
>
> RewriteEngine On
> RewriteBase /about/
> RewriteRule .* /frontend/handler.php?mode=about
> RewriteRule ^index.html$ /frontend/handler.php?mode=about
>
> is that a correct way to handle the overall situation?
> because the problem arises, when i'd like to additionally map an
> exception from another level of the path hierarchy, like mapping the
> actual homepage [/index.html] down to /frontend/handler.php?mode=home
> because obviously, then, the relative links to images, stylesheets etc
> are different and the page looks totally broken...
>
> how are you guys handling this?
>
> greets!


just in case that wasnt clear:

<Directory "/path/to/vhosts/server.com/public">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/handler.php [L,NS]
</Directory>

  #4 (permalink)  
Old 02-20-2007
Susanne West
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

shimmyshack wrote:
> On 19 Feb, 11:39, Susanne West <s...@gmx.de> wrote:
>
>>hi group.
>>
>>i've been using mod_rewrite for simple things so
>>far and always encountered the same (basic) problem,
>>which is, that relative paths get messed up.
>>
>>i was wondering if i'm doing something conceptionally wrong
>>if i use the following setup:
>>
>>i have multiple (empty!) directories on my disk that i'd like
>>to use as 'virtual' urls (e.g.):
>>
>> /about
>> /products
>> /detail
>>
>>as well as a special directory with a handler-script
>> /frontend/handler.php
>>that should be targeted by .htaccess-files in the directories
>>above. so each directory listed contains a .htaccess file with
>>the following (accordingly adapted) lines:
>>
>> RewriteEngine On
>> RewriteBase /about/
>> RewriteRule .* /frontend/handler.php?mode=about
>> RewriteRule ^index.html$ /frontend/handler.php?mode=about
>>
>>is that a correct way to handle the overall situation?
>>because the problem arises, when i'd like to additionally map an
>>exception from another level of the path hierarchy, like mapping the
>>actual homepage [/index.html] down to /frontend/handler.php?mode=home
>>because obviously, then, the relative links to images, stylesheets etc
>>are different and the page looks totally broken...
>>
>>how are you guys handling this?
>>
>>greets!

>
>
> If you just used
> ReWriteBase /
> ReWriteCond !-f
> ReWriteCond !-d
> RewriteRule . /frontend/handler.php
>
> and then used the value of $_SERVER['REQUEST_URI']
> you would be able to get all the info you needed, pasrsing the url
> will get you all the info into an array and then you can make the
> paths that you need to send and prepend them.
>
> My advice is to steer clear of relative urls in your html. What's the
> use of context driven resources (ie, ../../../../images/static/2.jpg,
> when you could just have /images/static/2.jpg - this way when you move
> your css directory you don't have a headache.
>
>
>



thanks for your suggestion. i will try that.

there is one drawback with your proposal of top-down urls such as
(/images/statis/2.jpg): i'm using a development system that (might)
have another base-url-path. like so:

development: 127.0.0.1/project_x/images/2.jpg
vs
productive: www.project_x.com/images/2.jpg

with relative urls, this is never a problem because the tree maps
from the given starting point. if you go top-down, you'd have to
rely on the base being the same...

greets.
  #5 (permalink)  
Old 02-20-2007
shimmyshack
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

On 20 Feb, 08:40, Susanne West <s...@gmx.de> wrote:
> shimmyshack wrote:
> > On 19 Feb, 11:39, Susanne West <s...@gmx.de> wrote:

>
> >>hi group.

>
> >>i've been using mod_rewrite for simple things so
> >>far and always encountered the same (basic) problem,
> >>which is, that relative paths get messed up.

>
> >>i was wondering if i'm doing something conceptionally wrong
> >>if i use the following setup:

>
> >>i have multiple (empty!) directories on my disk that i'd like
> >>to use as 'virtual' urls (e.g.):

>
> >> /about
> >> /products
> >> /detail

>
> >>as well as a special directory with a handler-script
> >> /frontend/handler.php
> >>that should be targeted by .htaccess-files in the directories
> >>above. so each directory listed contains a .htaccess file with
> >>the following (accordingly adapted) lines:

>
> >> RewriteEngine On
> >> RewriteBase /about/
> >> RewriteRule .* /frontend/handler.php?mode=about
> >> RewriteRule ^index.html$ /frontend/handler.php?mode=about

>
> >>is that a correct way to handle the overall situation?
> >>because the problem arises, when i'd like to additionally map an
> >>exception from another level of the path hierarchy, like mapping the
> >>actual homepage [/index.html] down to /frontend/handler.php?mode=home
> >>because obviously, then, the relative links to images, stylesheets etc
> >>are different and the page looks totally broken...

>
> >>how are you guys handling this?

>
> >>greets!

>
> > If you just used
> > ReWriteBase /
> > ReWriteCond !-f
> > ReWriteCond !-d
> > RewriteRule . /frontend/handler.php

>
> > and then used the value of $_SERVER['REQUEST_URI']
> > you would be able to get all the info you needed, pasrsing the url
> > will get you all the info into an array and then you can make the
> > paths that you need to send and prepend them.

>
> > My advice is to steer clear of relative urls in your html. What's the
> > use of context driven resources (ie, ../../../../images/static/2.jpg,
> > when you could just have /images/static/2.jpg - this way when you move
> > your css directory you don't have a headache.

>
> thanks for your suggestion. i will try that.
>
> there is one drawback with your proposal of top-down urls such as
> (/images/statis/2.jpg): i'm using a development system that (might)
> have another base-url-path. like so:
>
> development: 127.0.0.1/project_x/images/2.jpg
> vs
> productive:www.project_x.com/images/2.jpg
>
> with relative urls, this is never a problem because the tree maps
> from the given starting point. if you go top-down, you'd have to
> rely on the base being the same...
>
> greets.


well, if you are only doing markup, the its a valid method, but ways
exits to get the document root, and even the root of the application -
if you place it somewhere other than the doc root.
define( STR_APPLICATION_ROOT, dirname( _FILE_ ) );
#takes approx 0.0001 seconds to return the value inside your
config.inc.php file for the app.

download phpmyadmin or wordpress and so on, and se how they deal with
the multiplatform problem.

THe reason why it pays to actually know what these values are is that
you can keep part of your application out of the reach of prying eyes
by storing it in the /private/ directory
this way you cannot use ../../private because of the problem you
mention - you never know where the actual root of the site will be and
where private will be compared to that. But you can use the
dirname( _FILE_ ) method to find where things are or
dirname($_SERVER['SCRIPT_FILENAME'])
and so on... So while html might be ok with relative paths, it is
better to get the real paths from the underlying OS when writing a
complex php app.

I have a config.inc.php which includes path, environmental, language
and other settings for that system, and the rest of the app inherits
from that. You can then restructure your code and just change one
variable :)

  #6 (permalink)  
Old 02-20-2007
Susanne West
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

shimmyshack wrote:
> On 20 Feb, 08:40, Susanne West <s...@gmx.de> wrote:
>
>>shimmyshack wrote:
>>
>>>On 19 Feb, 11:39, Susanne West <s...@gmx.de> wrote:

>>
>>>>hi group.

>>
>>>>i've been using mod_rewrite for simple things so
>>>>far and always encountered the same (basic) problem,
>>>>which is, that relative paths get messed up.

>>
>>>>i was wondering if i'm doing something conceptionally wrong
>>>>if i use the following setup:

>>
>>>>i have multiple (empty!) directories on my disk that i'd like
>>>>to use as 'virtual' urls (e.g.):

>>
>>>> /about
>>>> /products
>>>> /detail

>>
>>>>as well as a special directory with a handler-script
>>>> /frontend/handler.php
>>>>that should be targeted by .htaccess-files in the directories
>>>>above. so each directory listed contains a .htaccess file with
>>>>the following (accordingly adapted) lines:

>>
>>>> RewriteEngine On
>>>> RewriteBase /about/
>>>> RewriteRule .* /frontend/handler.php?mode=about
>>>> RewriteRule ^index.html$ /frontend/handler.php?mode=about

>>
>>>>is that a correct way to handle the overall situation?
>>>>because the problem arises, when i'd like to additionally map an
>>>>exception from another level of the path hierarchy, like mapping the
>>>>actual homepage [/index.html] down to /frontend/handler.php?mode=home
>>>>because obviously, then, the relative links to images, stylesheets etc
>>>>are different and the page looks totally broken...

>>
>>>>how are you guys handling this?

>>
>>>>greets!

>>
>>>If you just used
>>>ReWriteBase /
>>>ReWriteCond !-f
>>>ReWriteCond !-d
>>>RewriteRule . /frontend/handler.php

>>
>>>and then used the value of $_SERVER['REQUEST_URI']
>>>you would be able to get all the info you needed, pasrsing the url
>>>will get you all the info into an array and then you can make the
>>>paths that you need to send and prepend them.

>>
>>>My advice is to steer clear of relative urls in your html. What's the
>>>use of context driven resources (ie, ../../../../images/static/2.jpg,
>>>when you could just have /images/static/2.jpg - this way when you move
>>>your css directory you don't have a headache.

>>
>>thanks for your suggestion. i will try that.
>>
>>there is one drawback with your proposal of top-down urls such as
>>(/images/statis/2.jpg): i'm using a development system that (might)
>>have another base-url-path. like so:
>>
>> development: 127.0.0.1/project_x/images/2.jpg
>> vs
>> productive:www.project_x.com/images/2.jpg
>>
>>with relative urls, this is never a problem because the tree maps
>>from the given starting point. if you go top-down, you'd have to
>>rely on the base being the same...
>>
>>greets.

>
>
> well, if you are only doing markup, the its a valid method, but ways
> exits to get the document root, and even the root of the application -
> if you place it somewhere other than the doc root.
> define( STR_APPLICATION_ROOT, dirname( _FILE_ ) );
> #takes approx 0.0001 seconds to return the value inside your
> config.inc.php file for the app.
>
> download phpmyadmin or wordpress and so on, and se how they deal with
> the multiplatform problem.
>
> THe reason why it pays to actually know what these values are is that
> you can keep part of your application out of the reach of prying eyes
> by storing it in the /private/ directory
> this way you cannot use ../../private because of the problem you
> mention - you never know where the actual root of the site will be and
> where private will be compared to that. But you can use the
> dirname( _FILE_ ) method to find where things are or
> dirname($_SERVER['SCRIPT_FILENAME'])
> and so on... So while html might be ok with relative paths, it is
> better to get the real paths from the underlying OS when writing a
> complex php app.
>
> I have a config.inc.php which includes path, environmental, language
> and other settings for that system, and the rest of the app inherits
> from that. You can then restructure your code and just change one
> variable :)
>



word!!! you are absolutely right! i have a global config file
which switches between development and integration systems and
actually, all i had to do, was to prepend my $urlbase variable
that carries '/' or then '/project_x/' instead of the relative
path ("../") et voila... thanks for pushing me there. i was
completely blindfolded by my own system & logic... ;)

that's the way to go and definately better than to go regex in
any PHP_SELF (or so) env-variable...


greets...




  #7 (permalink)  
Old 02-21-2007
Jim Carlock
 
Posts: n/a
Default Re: mod_rewrite and relative paths help

"shimmyshack" stated...
: if you are only doing markup, then it is a valid method,
: but ways exist to get the document root, and even the
: root of the application - if you place it somewhere other
: than the doc root.
:

<apache_code>
define( STR_APPLICATION_ROOT, dirname( _FILE_ ) );
# takes approx 0.0001 seconds to return the value inside
# your config.inc.php file for the app.
</apache_code>

Perhaps another way involves using Alias and the following
Apache code (inside httpd.conf)...

<IfModule mod_alias.c>
Alias /shared/ "C:/htdocs/www/root/shared/"

<Directory "C:/htdocs/www/root/shared">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</IfModule>

The above sets up a shared folder and it gets placed in the
httpd.conf file. Works well for a conglomeration of virtual
hosts where you want to share a specific folder in all hosts.

The corresponding PHP code...

//
// The shared folder holds a file named news.php that all
// virtual hosts can call upon and run.
//
// filename returns the whole path to the file and this works
// well PHP include() and require() functions.
//
$oNews = apache_lookup_uri('/shared/news.php');
$sPathToNews = $oNews->filename;
require($sPathToNews);

The above was implemented and tested with Apache 1.334
and 1.337 running on Windows XP in the year, 2006. Two
versions of PHP were tested successfully, PHP 4.3x & 5.2x.

I'm looking for some more help with mod_rewrite myself. If
anyone knows of any helpful things they can throw my way,
that would be greatly appreciated. Thanks!

--
Jim Carlock
Post replies to the group.


 
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

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



All times are GMT +1. The time now is 08:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0