htaccess rewrite breaks relative paths

The problem is that you didn’t consider that relative URLs are resolved on the base URI that is the URI of the HTML document the reference is used in. So a relative URI path like assets/images/ in an HTML document with the URI path /home/ is resolved to /home/assets/images/ instead of /assets/images/.

You cannot change this with mod_rewrite as URI resolution is done by the client and not by the server. The only solutions are:

  • change the base URI using the BASE element (note that this affects all relative URI);
  • using absolute URI paths, e.g. /assets/images/ instead of a relative assets/images/;
  • adjusting the relative URI path, so references in /home/ are adjusted to ../assets/images/ to reflect the path depth.

Leave a Comment