Emmanuel Gabion
Emmanuel Gabion

Reputation: 425

htaccess Resource interpreted as Stylesheet but transferred with MIME type text/html

Im trying to rewrite my existing url from http://localhost/website/article.php?id=5 to http://localhost/website/article/5 hence my htaccess code

RewriteBase /website/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule article/([[email protected]])? article.php?id=$1 [L]

What I get is Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/website/article/css/bootstrap.min.css".

It appears that article is added to the file's url, redirecting even the file directories.

The real url should be "http://localhost/website/css/bootstrap.min.css"

Im quite new to htaccess and trying to create my own by copying from existing ones

REVISION

My actual problem is that my resources url is being renamed as well. I've eliminated lines of codes down to 2 lines

RewriteEngine On
RewriteRule article/([[email protected]])  article.php?id=$1 

but now the resources is being redirected to non existent http://localhost/website/article/js/main.js instead of http://localhost/website/js/main.js

Upvotes: 3

Views: 1794

Answers (3)

chridd
chridd

Reputation: 161

Check the URLs in the main HTML file; I suspect the browser is requesting the wrong URL before it gets to the rewrite rules. If you have relative URLs like <script src="js/main.js">, then in the non-rewritten version the browser will resolve this to http://localhost/website/js/main.js, but in the rewritten version it will resolve to http://localhost/website/article/js/main.js, because from the browser's perspective the HTML file is in the directory http://localhost/website/article/, so it resolves relative to that. If this is the issue, you can change it to src="/website/js/main.js" or src="../js/main.js".

Upvotes: 2

Ivo P
Ivo P

Reputation: 1712

I prefer starting the rewrite rules with the lines:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule  - [L] 

It will exclude all exsisting files (-f) and directories (-d) from being rewritten. including the .php files and css files

Upvotes: 0

Elvis Plesky
Elvis Plesky

Reputation: 3300

Add rewrite condition to exclude static files from being rewritten:

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]

Upvotes: 0

Related Questions