可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
<!-- Using a absolute path. -->
<img src="/images/flag.png" />
<!-- Using a relative path. -->
<img src="../../../images/flag.png" />
However, this time I need to host the site at a non-root location e.g. http://my-server.com/holiday/
.
How would I go about this? I am looking for a solution that doesn't require me to change the path in my files. How do I configure the server (Apache) to treat http://my-server.com/holiday/
as a "root"?
Clarification:
I still need http://my-server.com/
to behave "normally". That is, it should still point to http://my-server.com/index.html
i.e. doesn't get redirected to http://my-server.com/holiday/
.
回答1:
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.
回答2:
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
回答3:
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
回答4:
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
回答5:
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
回答6:
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.
回答7:
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>
回答8:
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.