开发者

Relative Paths, etc. on an Apache server

I'm really stuck here.

I have 2 issues at once:

First, my site is stored (both on local development and on live server), in a subdirectory.. as I'm working on multiple sites. i.e.

/Sites/www.mysite.com/(site files here)

When I'm referring to files in my web pages, I want to refer to, say, my /images directory without hard-codi开发者_JAVA技巧ng every occurrence as /www.mysite.com/images/myfile.jpg

Is there a way to simply redefine how the leading "/" gets interpreted by the server?

Question two, concerning PHP mod_rewrite

I have this set of rewrite rules. The objective is to turn www.mysite.com/faq into www.mysite.com/index.php?page="faq"

RewriteEngine On
RewriteCond %{REQUEST_URI} !mysite.com
RewriteRule (.*) mysite.com/$1
RewriteRule ^([a-zA-Z0-9_-]+)$ /mysite.com/index.php?site=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ /mysite.com/index.php?site=$1

I don't have a problem when a url gets passed in the 2nd-to-last format (as the example above). However, if the trailing "/" is added: www.mysite.com/faq/, my external script references break: (such as src=js/script.js)...


To question no. 1:

Apache is interpreting leading slash according to DocumentRoot or Alias values

So if you want to make your life easier, create VirtualServer with this

DocumentRoot "/Sites/www.mysite.com"

Apache documentation: DocumentRoot

or Alias

Alias /mysite /Sites/www.mysite.com

Apache documentation: Alias

or use path-relative addressing.

To question no.2:

RewriteEngine On
RewriteCond %{REQUEST_URI} !mysite.com
RewriteRule (.*) mysite.com/$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)$ /mysite.com/index.php?site=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ /mysite.com/index.php?site=$1 [L]

according to netcoder advice.


Question 1: don't put a leading slash, and the path will be relative to your calling script's file location.

so if the calling script is in the directory

/www.mysite.com/myscript.php

and you use a filepath in your script of

images/myfile.jpg

this should work for what you are doing, and lead to

/www.mysite.com/images/myfile.jpg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜