1

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
  • 12,647
  • 4
  • 29
  • 41
kofifus
  • 163
  • 2
  • 8

1 Answers1

1

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]
MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 1
    tanks ! base tag is apparently not so trustworthy http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag – kofifus Mar 01 '17 at 05:50
  • and making my urls root relative is not an option, so I think I'll have to go for an external redirect – kofifus Mar 01 '17 at 05:51