DT.DTDG
DT.DTDG

Reputation: 765

htaccess RewriteBase not working

For development, I have my site setup in a subdirectory called "staging". All the JS and CSS references are pointing to /css/styles.css for example (notice the / before css).

The problem that occurs is when I am in a directory such as /staging/contact/ the contact page tried to load "/staging/contact/css/styles.css" rather it should be loading in "/staging/css/styles.css".

I have tried to rewritebase via htaccess however it's not working. I'm using:

RewriteEngine On
RewriteBase /staging/

When I view the source on the contact page (as it's not loading in any CSS/JS) it is still referencing /staging/contact/css/styles.css. What have I done wrong here? :/

Upvotes: 0

Views: 4483

Answers (2)

anubhava
anubhava

Reputation: 785266

Place this code in your DOCUMENT_ROOT/.htaccess (a level above /staging/):

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/staging/ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|ico|png|tiff|css|js)$ /staging%{REQUEST_URI} [L,R=301,NC]

Then no need to change your css/JS URLs and no need to use <BASE> tag in your HTML.

Upvotes: 2

Jon Lin
Jon Lin

Reputation: 143906

Your scripts and styles are probably linked using relative URI's. For example:

<link rel="stylesheet" href="css/style.css" type="text/css" />

You need to add a base to the header of your pages:

<base href="/staging/" />

Or, if neither of those are an option, you can try adding an additional rewrite rule to the top of your htaccess file:

RewriteBase /staging/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(css|js)/([^/]+)\.(js|css)$ $2/$3.$4 [R=301,L]

the htaccess file that is in staging.

Upvotes: 1

Related Questions