Reputation: 1057
I have a script that make search and I want the results also be accessible with query
mysite.com/searchfor/"myword"
I added next lines to .htaccess:
RewriteEngine on
RewriteRule ^/?searchfor/(.*)$ search.php?search=$1 [L]
It works (the page is loading), but every linked resource (css/js) failed to load with next error:
Resource interpreted as Stylesheet but transferred with MIME type text/html
Resource interpreted as Script but transferred with MIME type text/html:
These lines refer to mysite.com/searchfor/theme.css, but it is wrong, as the right link to the css is mysite.com/theme.css.
I suspect RewriteEngine giving wrong path to load styles and scripts what failed it. How to solve this?
Upvotes: 1
Views: 868
Reputation: 785771
Try this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^/?searchfor/(.+)$ search.php?search=$1 [L]
Upvotes: 1
Reputation: 96407
I suspect RewriteEngine giving wrong path to load styles and scripts what failed it.
Not the RewriteEngine is giving the wrong paths – your HTML is.
If you refer your scripts and stylesheets relative to the path the client has requested, then it of course make a difference whether the client requested example.com/foo
or example.com/searchfor/bar
– since relative paths are resolved in relation to the location of the current document.
How to solve this?
The easiest way is to reference external resources relative to the domain, beginning with a slash – instead of script.js
you use /script.js
, that tells the client that he has to complete this path with the domain only.
Upvotes: 0