Apache URL rewriting error: CSS & JS files not loading (4 solutions)

Updated: January 20, 2024 By: Guest Contributor Post a comment

The Problem

If you’re developing for the web, you might have encountered a situation where your stylesheets and JavaScript files are not loading due to Apache’s URL rewriting rules. This issue usually arises when using ‘mod_rewrite’ for clean URLs—resulting in your applications not correctly referencing CSS or JS files. In this tutorial, we’ll discuss the common causes of this error and provide you with solutions to fix it.

Understanding the URL Rewriting Error

At its core, this error is due to an Apache misconfiguration. ‘mod_rewrite’ is an Apache module used to alter URLs before they are processed by the webserver. It’s generally used to create human-readable URLs. However, if not correctly configured, the rewrite rules can unintentionally redirect requests for static files, causing CSS and JS files to not load properly.

Solutions

Check RewriteBase Directive

Ensure that the ‘RewriteBase’ directive is set correctly in your .htaccess file. This directive tells Apache the base URL to consider when applying the rewrite rules. If your application resides in the root directory, ‘/ ‘ should be the base. If it’s in a subdirectory, ‘/subdirectory/’ should be the base.

Steps:

  1. Open the .htaccess file in the root directory of your site.
  2. Find the line that begins with ‘RewriteBase’.
  3. Confirm that the path matches the location of your application’s index file.
  4. Save the changes and upload the file to the server if editing locally.
  5. Clear the browser cache and refresh your webpage to test changes.

Example:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Notes: Using the correct ‘RewriteBase’ can greatly simplify your rewrite rules and prevent many common issues with file loading. However, it won’t resolve issues related to the presence of ‘RewriteCond’ directives that filter out file requests, something which the next solutions address.

Exclude Directories and Files From Rewriting

Add conditions to your rewrite rules to exclude CSS and JS files. Rewrite conditions (`RewriteCond`) instruct Apache to only apply ‘RewriteRule’ when the conditions are met. The negation pattern ‘!’ is used to exclude files and directories.

Steps:

  1. Open your .htaccess file.
  2. Add conditions to exclude specific file extensions before the existing rewrite rules.
  3. Save and re-upload your .htaccess file.
  4. Test your changes.

Example:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|js)$
RewriteRule ^ index.php [L]

Notes: While this method is robust and works well, you must be careful to include all file types you wish to exclude. Missing a file type can lead to other files not loading properly.

Relative Path Fix in HTML

Use relative paths for CSS and JS files in your HTML. A common issue with rewrite rules is that absolute paths might point to the wrong location. Ensuring that the references in your HTML are relative to the location that the files are served from can mitigate issues related to URL rewriting.

List of Steps:

  1. Open your HTML file.
  2. Find the link and script tags referencing your CSS and JS files.
  3. Modify the href/src attributes to use relative paths.
  4. Save your changes and refresh to test.

Notes: Relative paths ensure that your files are loaded relative to the requesting document’s path. However, if your document’s URL changes (for instance, through new rewrite rules), you’ll need to ensure the relative paths remain correct. In complex applications, path resolution errors can happen if the directory structure isn’t managed carefully.

Use the HTML Tag

The tag specifies a base URL for all relative URLs in your HTML. If you add a base tag in your HTML files, all subsequent relative links will resolve from that URL.

Steps:

  1. Open the head section of your HTML file.
  2. Add a tag with the ‘href’ attribute set to the root URL of your site.
  3. Ensure that the base tag appears before any link or script tags.
  4. Save and refresh the browser to test the change.

Example:

<head>
    <base href="http://example.com/">
    ...Additional meta, link, and script tags...
</head>

Notes: Although the tag is easy to implement and allows you to easily manage your relative URLs, it will affect every relative URL in your document. This broad effect can have unintended consequences elsewhere if you’re not careful to account for all relative URL references throughout your site.

Conclusion

Errors with CSS and JS file loading are often due to misconfigured rewrite rules in Apache. By paying close attention to the ‘RewriteBase’ directive, excluding appropriate files and directories from being rewritten, fixing HTML paths, or using the HTML tag, you can resolve these errors. Always be sure to test your changes on a development server before deploying to production, and thoroughly check for unintended side effects. With the right approach, you can have your web resources loading perfectly alongside clean and friendly URLs.