Apache 服务器上的相对路径等

发布于 2024-10-10 07:55:40 字数 790 浏览 0 评论 0原文

我真的被困在这里了。

我同时有两个问题:

首先,我的网站存储在子目录中(在本地开发和实时服务器上)。因为我正在多个网站上工作。即,

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2024-10-17 07:55:40

问题编号。 1:

Apache 根据 DocumentRootAlias 值解释前导斜杠

,因此,如果您想让生活更轻松,请创建 VirtualServer使用此

DocumentRoot "/Sites/www.mysite.com"

Apache 文档:DocumentRoot

Alias

Alias /mysite /Sites/www.mysite.com

Apache 文档:别名

或使用路径相对寻址。

问题 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]

根据 netcoder 建议。

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.

蛮可爱 2024-10-17 07:55:40

问题 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文