Apache 服务器上的相对路径等
我真的被困在这里了。
我同时有两个问题:
首先,我的网站存储在子目录中(在本地开发和实时服务器上)。因为我正在多个网站上工作。即,
/Sites/www.mysite.com/(site files here)
当我引用网页中的文件时,我想引用我的 /images
目录,而不是将每个出现的地方硬编码为 /www.mysite.com/images /myfile.jpg
有没有一种方法可以简单地重新定义服务器如何解释前导“/”?
问题二,关于PHP mod_rewrite
我有这套重写规则。目标是将 www.mysite.com/faq 转换为 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
当 url 以倒数第二个格式传递时,我没有问题(如上面的例子)。但是,如果添加尾部“/”:www.mysite.com/faq/
,我的外部脚本引用将中断:(例如src=js/script.js
)...
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-coding 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
)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题编号。 1:
Apache 根据
DocumentRoot
或Alias
值解释前导斜杠,因此,如果您想让生活更轻松,请创建
VirtualServer
使用此Apache 文档:DocumentRoot
或
Alias
Apache 文档:别名
或使用路径相对寻址。
问题 2:
根据
netcoder
建议。To question no. 1:
Apache is interpreting leading slash according to
DocumentRoot
orAlias
valuesSo if you want to make your life easier, create
VirtualServer
with thisApache documentation: DocumentRoot
or
Alias
Apache documentation: Alias
or use path-relative addressing.
To question no.2:
according to
netcoder
advice.问题 1:不要放置前导斜杠,路径将相对于调用脚本的文件位置。
因此,如果调用脚本位于目录
/www.mysite.com/myscript.php
的文件路径,则
中,并且您在脚本中使用images/myfile.jpg
这应该适用于您正在执行的操作,并导致
/www.mysite。 com/images/myfile.jpg
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