Timothy Fisher
Timothy Fisher

Reputation: 1129

How can I change the directory a relative path points to in apache?

I have a website running on an apache2 server and serving out of a user directory. The URL looks like this: http://example.com/~user1

This serves up files on my server located at /home/user1. The problem that I am facing is that a lot of the asset URLs throughout the site are put into the HTML as relative paths like this: <img src="/images/example.png" alt="" />

This is attempting to load the image from http://example.com/images/example.png, but I actually want it to serve from http://example.com/~user1/images/example.png.

How can I accomplish this?

Upvotes: 1

Views: 428

Answers (1)

MrWhite
MrWhite

Reputation: 45914

/images/example.png

This is a root-relative, not a relative URL-path.

This is attempting to load the image from http://example.com/images/example.png, but I actually want it to serve from http://example.com/~user1/images/example.png.

This is a client-side/browser URL resolution issue. There are no "switches" on the server that can be applied to change this behaviour, without changing the actual URLs.

This is an inherent problem with using Apache per-user web directories.

Your options are:

  1. Don't use Apache per-user web directories. ie. Remove /~<user1>/ from the URL.

Or,

  1. Use relative URL-paths to your assets. eg. images/example.png (no slash prefix). So the browser correctly resolves this as /~user1/images/example.png instead (when accessed from the document root at least). But this creates additional problems if you are rewriting the URL of your pages at different path depths or using common templates in your pages etc.

Upvotes: 1

Related Questions