Search code examples
.htaccessmod-rewriterelative-pathabsolute-path

Relative paths break in HTML when using mod_rewrite in .htaccess


I am new to .htaccess and mod_rewrite.

I am using the following rewrite rules:

RewriteRule ^wants/([0-9]+)$ /wantlist.php?wantid=$1
RewriteRule ^dashboard$ /dashboard.php

I am able to successfully redirect both the cases.

But in order to do this, I had to change the CSS, JS and other paths to absolute paths without which the CSS and JS did not load.

I was using

<script type="javascript" src="js/javascript.js"></script>

which after the rewrite rule did not work and after I changed it to

<script type="javascript" src="http://example.com/js/javascript.js"></script>

I got it to work.

The same is happening for any of the other URLs on the website.

Is there a way to bypass this as I am using relative paths almost everywhere in the site I am developing?


Solution

  • This is an URL resolving issue. See mod_rewrite URL info required

    You could explicitly set a base URI with the BASE HTML element, for example:

    <base href="/">