Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts

    ModRewrite affecting DOCUMENT_ROOT

    following on from Add-on domain modrewrite headache 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...):

    Code:
    <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.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Another reason not to use document_root, and to use relative includes instead.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    how would i go about doing that?

    do you mean instead of

    PHP Code:
    $_SERVER['DOCUMENT_ROOT'] . '/folder/header.php' 
    use

    PHP Code:
    '/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... ?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by pepsi_max2k View Post
    how would i go about doing that?

    do you mean instead of

    PHP Code:
    $_SERVER['DOCUMENT_ROOT'] . '/folder/header.php' 
    use

    PHP Code:
    '/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');.
    Quote Originally Posted by pepsi_max2k View Post
    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.

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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 ../:

    Code:
    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

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,023
    Thanks
    2
    Thanked 314 Times in 306 Posts
    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 Code:
    <?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';
    ?>
    Last edited by CFMaBiSmAd; 11-01-2007 at 11:00 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    >>// 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 "


    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... :|
    Last edited by pepsi_max2k; 11-02-2007 at 10:34 AM.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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 Code:
    <?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.

  • #9
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,023
    Thanks
    2
    Thanked 314 Times in 306 Posts
    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.
    Last edited by CFMaBiSmAd; 11-02-2007 at 06:40 PM. Reason: add more
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,023
    Thanks
    2
    Thanked 314 Times in 306 Posts
    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.
    Last edited by CFMaBiSmAd; 11-02-2007 at 06:41 PM. Reason: add summery
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    New Coder
    Join Date
    Mar 2004
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:

    Code:
    <?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 ):

    Code:
    <?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.

  • #13
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •