大网数据广告活动
服务器运维问答

apache alias pages that load relative resources

· 52CE 我爱测

I am trying to alias a path to a specific file I tried: Alias /Console "/console/console.html" Now going to http://example.com/Console indeed brings console.html . but any relative resource referenced from within console.html (ie…

问题原文

原帖链接 · kofifus · CC BY-SA 3.0

I am trying to alias a path to a specific file

I tried:

Alias /Console "/console/console.html"

Now going to http://example.com/Console indeed brings console.html.

but any relative resource referenced from within console.html (ie, images\1.jpg) fails as it seems to go to the document_root

What am I doing wrong?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

This is a client-side problem related to your use of relative URLs. Since you are effectively rewriting the URL from /console/console.html to /Console, any relative URL (to images, CSS, JS, etc.) are now going to be relative to the document root and not the /console subdirectory.

You need to either "fix" your relative URLs and make them root-relative (ie. starting with a slash, as in /console/images/1.jpg) or make them absolute.

Alternatively, you can include a base element in the head section of your HTML document. This references the absolute URL that all relative URLs are relative to. In other words, since you are expecting these relative URLs to be relative to the /console/console.html document then add the following to the head section:

<base href="http://example.com/console/console.html">

Now, a relative URL such as images/1.jpg referenced in a document at URL /Console will request http://example.com/console/images/1.jpg, not http://example.com/images/1.jpg.

However, there are caveats with using the base element. Notably, if you have in-page relative links using fragment identifiers, eg href="#some-id". This is also now relative to your base element so the in-page link may now be broken. The only way round this is to fully qualify the link.

Reference:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base


However, Alias is not the correct directive to use when mapping to a single file, since Alias is prefix matching, so /Console/foo would map to /console/console.html/foo as well. You could perhaps use AliasMatch instead:

AliasMatch ^/Console$ /console/console.html

This will map only /Console.

Or, you could use mod_rewrite instead and issue an internal rewrite. (As opposed to an external redirect that would otherwise change the URL and cause the client to issue a second request.) For example:

RewriteEngine On
RewriteRule ^/?Console$ /console/console.html [L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。