Absolute Path for Deployment in a Non-Root Location

大城市里の小女人 提交于 2019-12-01 00:45:22

If you’re using Apache, there one rather simple thing you could do by just using an SSI variable in your paths. Do a global replace of all src="/ to something like

src="<!--#echo var="prefix" -->/

and then in your htaccess for the specific folder define the prefix variable as /holiday

For sites that don’t have the variable or SSI, it’ll just show up as a comment or you can define it as an empty string.

Of course this means you’ll have to turn on SSI in Apache.

Zoltan Toth

try to add this to your .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?DOMAINNAME.com$
RewriteCond %{REQUEST_URI} !^/holiday/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /holiday/$1
RewriteCond %{HTTP_HOST} ^(www.)?DOMAINNAME.com$
RewriteRule ^(/)?$ holiday/index.php [L]

or take a look here - Changing the root folder via .htaccess

I don't think this is gonna happen for you man, sorry.

You have absolute paths in your documents, and want apache to prepend /holiday to them all without effecting the actual docroot? You can have one or the other, but not both.

You need to either do a mass edit and prepend the directory yourself, or move your images/css/etc into the actual root directory.

mod_rewrite is powerful, but can't really determine intent and parse the same url two different ways depending on what the user wants.

Edit:

I am wrong, but I don't have your answer. You may be able to use IS_SUBREQ in mod_rewrite to only apply the re-write conditions for sub-requests from your /holiday/index.php

Easy...

# Assuming mod_rewrite is an option...
<IfModule mod_rewrite.c>
   # Turn it on!
   RewriteEngine on

   # If path is /images/flag.png, connect to /holiday/images/flag.png
   RewriteBase /holiday/
</IfModule>

Assuming I'm understanding what you mean, this should do you just fine. Point of order, this .htaccess should be in /holiday/

I do this locally on MAMP for testing a website that's base is in http://localhost:8888/SocialNetwork/ ... If I didn't have that, my absolute paths of, say, /profile would go to http://localhost:8888/profile/ instead of http://localhost:8888/SocialNetwork/profile

what about using html's BASE element? http://www.w3.org/TR/html4/struct/links.html#h-12.4

although i´m not sure how you could have it in a single html file and inherit it to the rest of your htmls, so your source remains intact. if your site is html-only maybe with frames, otherwise you could use some sort of server-side include depending on what youre using (asp, jsp, whatever). Check out this link for more information http://www.boutell.com/newfaq/creating/include.html

moey

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.

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>

If you have a list of filename extensions that should be redirected, you might want to use the rewrite conditions using pattern matching againt the extensions.

Another solutions is to simply create an /images directory under root and let images be downloaded from there. Or have there links to images in your path.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!