Absolute Path for Deployment in a Non-Root Location

后端 未结 8 1303
耶瑟儿~
耶瑟儿~ 2021-01-06 13:22

I typically refer to any assets on my site using absolute path so that I don\'t have to worry about the location of the assets relative to current file.

<         


        
相关标签:
8条回答
  • 2021-01-06 13:55

    EDIT:

    Of course it would work in case you do with php. Adding:

    <? define(_BASE_, substr($_SERVER['SERVER_NAME'] . "/" . $_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/" )) ); ?>
    
    <html>
    
        <head>
            <script src="<?=_BASE_?>/path_relative_to_project_root"></script>
        </head>
    
        <body>
            <img src="<?=_BASE_?>/path_relative_to_project_root">
        </body>
    
    </html>
    
    0 讨论(0)
  • 2021-01-06 13:57

    I hate to say this but none of the above provides the solution. The answers by @Zoltan and @stslavik were close but, unfortunately, didn't work when deployed; I wished one of them worked, though.

    As a result, I resorted to using a combination of named constants and include files in PHP. More details: File Structure for a PHP Project. Note that it doesn't have to be PHP; you can use other languages that provide similar features. +1 for @margusholland whose answer led me to experiment with this solution.

    0 讨论(0)
提交回复
热议问题