I'm having issues with this modrewrite rule I'm applying.

It works as intended, but when viewing the page, it's expecting an absolute path for all my scripts/css/images. So those won't display unless I go through each one individually and prepend "../" to them (i.e. 'icons/book.gif' => '../icons/book.gif')

Is there a way to fix this with modrewrite itself? Here is the rule I'm applying:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule  ^category/([A-Za-z\+]+)/?$ fact.php?category=$1
</IfModule>

EDIT: This function will determine the absolute path, which is recommended to use for all links on the site.

// determines and returns the absolute url path
function absolute_url ($page = "index.php") {
    $url = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

    // remove trailing slashes
    $url = rtrim($url, '/\\');

    // add the page
    $url .= "/" . $page;

    return $url;

}

Accepted Answer

Have your rules like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^category/([A-Za-z\+]+)/?$ fact.php?category=$1 [L,NC]

This will avoid rewriting for files present (static files like css, js, images).

Alternatively you can use HTML base tag like this for static files:

<base href="http://www.example.com/static/" />

However it is recommended to always use absolute path for static files.

Written by anubhava
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki