...

View Full Version : ModRewrite affecting DOCUMENT_ROOT



pepsi_max2k
11-01-2007, 07:06 PM
following on from http://www.codingforums.com/showthread.php?t=126931 as i've got a better idea of what's happening now...

I'm using <?php include($_SERVER['DOCUMENT_ROOT'] . '/folder/header.php'); ?> in some pages of mysite.com/folder, which would normally resolve to /home/inaueco/public_html/folder/header.php

But I've switched to using an addon domain for the folder in question, and am using the following mod rewrite code to get rid of the /folder/ in the urls (and a bit of other stuff...):


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^crimea/(.*)$ /$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


thing is, now instead of resolving to the normal public_html/folder/header.php, instead the same php code is resolving to /home/inaueco/public_html/folder/folder/header.php

you can see an example of this at
http://www.inaudible.co.uk/crimea/pages/downloads.html
http://www.immortalsoon.co.uk/pages/downloads.html
where i'm just using the document_root bit (no . ' folder...') and the second one shows that it's somehow adding in the /folder/ part (actually /crimea), even though it's not included in the php include code.

any ideas why, and how i can solve it? my huntch is the modrewrite is adding it in to the php include code for some reason, when it doesn't do this to normal html code.

thanks.

aedrin
11-01-2007, 07:28 PM
Another reason not to use document_root, and to use relative includes instead.

pepsi_max2k
11-01-2007, 07:40 PM
how would i go about doing that?

do you mean instead of


$_SERVER['DOCUMENT_ROOT'] . '/folder/header.php'

use


'/home/user/public_html/folder/header.php'

? would that not just tread the /home/... like it's a regular url? ie. giving the effect of mydomain.com/home/user/public_html/folder... ?

Inigoesdr
11-01-2007, 08:25 PM
how would i go about doing that?

do you mean instead of


$_SERVER['DOCUMENT_ROOT'] . '/folder/header.php'

use


'/home/user/public_html/folder/header.php'

That would work, but that's not what he was talking about. aedrin meant something like include('../folder/config.php');.

would that not just tread the /home/... like it's a regular url? ie. giving the effect of mydomain.com/home/user/public_html/folder... ?
No, because you're working with file paths.

pepsi_max2k
11-01-2007, 10:36 PM
hmm... file paths are slightly better (they work), but then pose the problem of moving hosting providers and having a different user name, and therefore different file path, and that's only if they use the same /home/username/public_html/ structure. that's why i used the document_root bit to start with.

../'s just give the following error, seemingly literally interpreting the ../:


Warning: main() [function.main]: open_basedir restriction in effect. File(../crimea/pages/header.php) is not within the allowed path(s): (/home/inaueco:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/username/public_html/crimea/pages/downloads.html on line 1


figuring out why it was adding dual /crimea folders would be a little more useful ;)

CFMaBiSmAd
11-01-2007, 10:54 PM
Rewriting the url "should not" affect the $_SERVER['DOCUMENT_ROOT'] variable, but we don't live in a perfect world. Perhaps your version of web server/php contains a bug?

Create a configuration "defined constant" that you set to the correct DOCUMENT_ROOT path and use it in place of $_SERVER['DOCUMENT_ROOT'] in your code. A lot of the popular PHP scripts do this, perhaps for the same reason as the problem you are having (rewriting the url.)

Edit: Like this (tested) -

<?php
// define the root path in your config file
define("DOC_ROOT", "/home/user/public_html"); // what ever it happens to be

// then in any file -
include DOC_ROOT . '/folder/header.php';
?>

pepsi_max2k
11-02-2007, 01:07 AM
>>// define the root path in your config file

:( any clues? i guess you meant php.ini? or would .htaccess do? and would i need the <?php > bits if it was in there? this kinda stuff is hard to get off google, "php config file define" don't get you too far :rolleyes: "


edit: well it works, at least when the codes in the same file. now just need to figure out where to put the define for it to be site wide... isp site suggests php.ini is for php5, 4 just need .htaccess but the only php code i can find for that has php_value or somesuch attached... :|

aedrin
11-02-2007, 03:52 PM
hmm... file paths are slightly better (they work), but then pose the problem of moving hosting providers and having a different user name, and therefore different file path, and that's only if they use the same /home/username/public_html/ structure. that's why i used the document_root bit to start with.

Where is the username/changing part in:



<?php
include('../common/config.php');
?>


I can't think of any downside to this method, or any upside to the document_root method over this.

pepsi_max2k
11-02-2007, 05:21 PM
the document_root method doesn't work. due to whatever reason, probably something to do with a modrewrite of urls, it's doubling up whatever folder comes up after the root part. which is the reason for the workarounds mentioned above.


the username bit is in the

define("DOC_ROOT", "/home/user/public_html");

(with user being my user name on the server). and at the moment i only know how to get that code working if i physically add it in to every single page just before the include code. which 1) defeats the point needing to define it in a constant, and 2) defeats the point of needing the include at all.


i've tried adding "php_value doc_root "/home/user/public_html"" to various .htaccess files but it does nothing, regardless of doc_root case or quotation marks. i've tried adding my own php.ini file but even if that would work i think i need to include the php.ini somewhere else, and then it would overwrite the default php.ini anyway and muck everything up (which only the hosting provider has access to, though i can request changes if needed). i've tried adding it in to my wordpress install's wp-config.php that also has define("... values in and it does nothing. i've tried searching google but i don't get far as the only decent info relates to the above methods.
i assume the CFMaBiSmAd knows where it should be put, as he suggested it originally, it'd just be nice to know too :)

CFMaBiSmAd
11-02-2007, 05:45 PM
My suggestion before was to include a configuration file with the define() statement in it. Only your main file(s) need to include it and it will be defined for the scope of those files and any files they include. Includes normally work and are not affected by url rewriting. I don't think we have an accurate picture of what your code actually has in it. Something is missing. Edit: Also, a configuration file is often in the same folder as the file using it and the include would not contain any path information.

How about using the include_path = ... statement and setting it in a .htaccess file (though if the url rewrite is affecting file systems paths, this won't work either.)

You can also set the DOCUMENT_ROOT variable in a .htaccess file. Perhaps force it to what you want it to be after all the rewrite rules have been applied.

On a more basic level, instead trying to work around this problem (your's is the first time I have seen this problem), find out if there is a bug with your version of web server/php that is causing it or if there is something else you are doing that could be causing it.

Is there any chance you have more than one level of .htaccess file that is rewriting the url? One in your document root and one in a sub folder? Is the content you showed for your .htaccess file everything in it?

Short answer - this seems to be something specific to your situation, or searching for the problem would have turned up relevant information or someone here would have seen it before.

CFMaBiSmAd
11-02-2007, 06:11 PM
Upon review of your last 2-3 threads on includes/domains/rewriting, I have a probable answer -

Whatever was done to add the second domain and have it point to your crimea sub-folder, modified the DOCUMENT_ROOT variable. Therefore, when you use $_SERVER['DOCUMENT_ROOT'] in your code it already has the crimea sub-folder on the end of it.

Edit: So, it is not actually the url rewriting that you showed that is causing this, but what was done for the second domain name.

pepsi_max2k
11-07-2007, 09:35 AM
Ok thanks for that. This is what I have in my .htaccess's. Both public_value things do nothing, just not bothered to lose them yet...

publich_html/.htaccess:




AddHandler application/x-httpd-php .html .htm


php_value doc_root "/home/username/public_html"


public_html/crimea/.htaccess:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^crimea/(.*)$ /$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect 301 /crimea http://www.immortalsoon.co.uk

php_value doc_root "/home/username/public_html"

everything but the following was already there (used with a wordpress install):

RewriteRule ^crimea/(.*)$ /$1 [R=301,NC,L]
Redirect 301 /crimea http://www.immortalsoon.co.uk

2nd is to redirect away from /crimea, first is to get rid of /crimea in any links due to them not working with the redirect.



And this is all I had in my main content pages which worked before the change:


<?php include($_SERVER['DOCUMENT_ROOT'] . '/crimea/pages/header.php'); ?>

<style type="text/css">
...
</style>

<H1>Upcoming Gigs</H1>


And this is my index.php which does still work (again it's wordpress auto-done so i couldn't mess it up :) ):



<?php
/* Short and sweet */
define('WP_USE_THEMES', true);
require('../blog/wp-blog-header.php');
?>


Finally recovering from my PHP dimness, include( 'header.php' ) for files in /crimea/pages works fine, and erm... adding '../../header.php' to those in deeper folders would be no different to using docroot/crimea/pages/header.php? Ie. a change of location for header.php would need them all updating, though i guess if I moved any pages to a different directory they *would* need their includes changing...


Anyway, if you're able to solve the orignial problem (dual /crimea folders) then please do, as lord knows what else it's messing up. But at least I have a suitable work around for now...

Thanks.

Denyer
12-13-2007, 06:29 PM
Been a while since I've done anything on the subject, so hopefully someone else can confirm... Redirect is part of mod_alias, the [L]s would have no effect on it or the data being passed to it, right? Again, haven't tested, but I'd guess that's the source of the loop.

If so... Chris, you need to give people full information... ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum