When I write my .htaccess file the css and js of my project don't work. I use the MVC pattern and in my mainController I require_once every time the view. I don't understand, why with .htaccess file the program tries to require_once also the js.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)$ index.php?controller=$1&action=$2 [QSA,L]
This is the console output:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "".
jquery.min.js:1 Uncaught SyntaxError: Unexpected token <
bootstrap.min.js:1 Uncaught SyntaxError: Unexpected token <
Please add mime header types in your .htaccess file, maybe it help
AddType text/css .css
AddType text/javascript .js
Related
My customer is using Litespeed with CPanel v106.0.10.
I have a RewriteRule like this in .htaccess file:
RewriteCond %{HTTP_HOST} ^www.site.com$
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}%{ENV:REWRITEBASE}/img/$1.webp -s
RewriteRule ^img/([_a-zA-Z0-9-]+)\.jpg$ %{ENV:REWRITEBASE}img/$1.webp [B,L]
RewriteCond %{HTTP_HOST} ^www.site.com$
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}%{ENV:REWRITEBASE}/img/$1.webp !-s
RewriteRule ^img/([_a-zA-Z0-9-]+)\.jpg$ %{ENV:REWRITEBASE}webp.php?src=img/$1.jpg [B,L]
The goal is to serve images into WEBP format when the WEBP file exists, if not then call the PHP script to compress the JPG file into WEBP format.
It's working good with Apache but when using Litespeed server I get a 403 error when the WEBP file does not exists.
Also when the URL of the image does not match the real file name (URL rewriting) then it also works.
So I think there is something that blocks URL rewriting when the URL match with a real file.
Who can help?
I found that this is a difference between Apache and Litespeed.
Litespeed does not prioritize the rules defined in .htaccess files in the same way as Apache. Thus PHP files are forbidden in the "/img" directory, when the URL "/img/image.webp" arrives, Apache applies the redirection to the PHP compressor but does not apply the "access denied" on the script because it is not in the "/img" directory, whereas Litespeed applies the redirection to the PHP compressor but blocks access to it (403) because the original URL starts with "/img".
I guess that this is a bug from Litespeed.
I generated *.webp files that are named exactly like their png or jpg source. Then I added this to my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Does browser explicitly support webp?
RewriteCond %{HTTP_USER_AGENT} Chrome [OR]
# OR Is request from Page Speed
RewriteCond %{HTTP_USER_AGENT} "Google Page Speed Insights" [OR]
# OR does this browser explicitly support webp
RewriteCond %{HTTP_ACCEPT} image/webp [OR]
# AND does a webp image exists?
RewriteCond %{DOCUMENT_ROOT}/$1\.webp -f
# THEN send the webp image and set the env var webp
RewriteRule (.+\.(?:jpe?g|png))$ $1.webp [NC,L]
</IfModule>
<IfModule mod_mime.c>
AddType image/webp .webp
</IfModule>
What I expect:
all jpg files load normal e.g /test/marc.jpg If a webp with the same name exists, serve a webp file via url /test/marc.jpg
This works of the webp file exists. But if I delete a webp file I get a 404 on the jpg or png url. Even old jpg or png urls then give a 404. Example:
/www/media/
.htaccess
marc.jpg
marc.webp
http://domain/marc.jpg serves the webp. I test this from chrome with cache disabled. When I delete the marc.webp I get a 404 on http://domain/marc.jpg Why? RewriteCond %{DOCUMENT_ROOT}/$1.webp -f should fix this, right?
These two lines are not looking for the files you describe:
# AND does a webp image exists?
RewriteCond %{DOCUMENT_ROOT}/$1\.webp -f
# THEN send the webp image and set the env var webp
RewriteRule (.+\.(?:jpe?g|png))$ $1.webp [NC,L]
In both of these, $1 refers to the first captured match in the RewriteRule, which in this case is (.+\.(?:jpe?g|png)). If you request, "marc.jpg", that whole string matches, and will be placed in $1. The two lines therefore evaluate as:
If the file "marc.jpg.webp" exists, respond with "marc.jpg.webp"
Since it doesn't, the rule will not be run.
The condition you wanted was:
If the file "marc.webp" exists, respond with "marc.webp"
So you want $1 to contain only the "marc" part of the requested file; that's just a matter of moving the closing parenthesis:
RewriteRule (.+)\.(?:jpe?g|png)$ $1.webp [NC,L]
This doesn't explain why your rule appeared to work, and stopped working when you deleted a file. I suspect you have another rule somewhere else which is confusing the situation.
And happy 2016 ;-)
I'm actually trying to learn ZF2, and I have some problems that I dont' know how to resolve.
First, you have to know that the project is running under vagrant.
I have 2 modules:
Admin
Application
I have a layout for each module.
In the admin layout module, I have some style and JS file:
All this JS and CSS files are in the public folder.
<?= $this
->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath('bower_components/bootstrap/dist/css/bootstrap.min.css'))
->prependStylesheet($this->basePath('bower_components/font-awesome/css/font-awesome.min.css'))
->prependStylesheet($this->basePath('bower_components/Ionicons/css/ionicons.min.css'))
->prependStylesheet($this->basePath('css/AdminLTE.min.css'))
->prependStylesheet($this->basePath('css/skins/skin-blue.min.css'))
; ?>
<?=
$this->inlineScript()
->prependFile($this->basePath('bower_components/jquery/dist/jquery.min.js'))
->prependFile($this->basePath('bower_components/bootstrap/dist/js/bootstrap.min.js'))
->prependFile($this->basePath('js/app.min.js'))
;?>
For some CSS file, I have errors in the browser:
Resource interpreted as Stylesheet but transferred with MIME type
text/html
And for JS, I have problems too:
Bootstrap's JavaScript requires jQuery
I know this JS error, it append when you load boostrap.js before jquery.js.
But here, it's not the case!
I don't know if the problem comes from my layout integration of CSS and JS files, or if I forgot something in the controller, or in the configuration.
May be it's a problem with vagrant too?
I also have a htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Please, I need some help because i'm lost ;-)
Thanks for any advice/documentation or else.
I have a hosting account with webfusion.co.uk where I host a few websites. I have uploaded a robots.txt file and a sitemap.xml file. when I try visiting them: www.ledflexi.co.uk/robots.txt or www.ledflexi.co.uk/sitemap.xml. The site returns a 404 error.
I have uploaded both files to the root folder of each site (public_html/led-flex.co.uk)
Could the following lines of the .htaccess file be related to this problem?
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
############################################
## Prevent character encoding issues from server overrides
## If you still have problems, use the second line instead
AddDefaultCharset Off
#AddDefaultCharset UTF-8
<IfModule mod_expires.c>
############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
ExpiresDefault "access plus 1 year"
</IfModule>
############################################
## By default allow all access
Order allow,deny
Allow from all
########################Redirection############################
please check the file name, in linux servers if the name is not in the exact case that you have typed. it will return 404. make sure the filename is 'robots.txt' (all small case)
Thanks to vilsad for replying to the question.
I found the problem, and though I'm a bit ebarrassed by it here it is: I was Uploading to the wrong accound : "led-flex.co.uk" instead of "ledflexi.co.uk".
What a Klutz!
I'm wondering if it's possible to use mod rewrite along with the ErrorDocument deceleration to customize the error pages depending on what type of file is requested.
For example a non-existent html or php file is requested Apache will give nice custom HTML page.
But if a non-existent image, js, css, etc... file is requested then Apache will serve out a basic html file with only a link on it.
A good example of this behavior is Facebook. Try requesting a bogus JavaScript file, you will receive a different page then if you were to request a non-existent php file.
Ended up using a combination of both ErrorDocument and RewriteRules this works because the php page throws a 404 Not Found for me.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*(?<!.js|.css|.ico|.txt|.bmp|.gif|.png|.jpeg|.jpg)$ /error/404.php [L]
ErrorDocument 404 /404_basic.html
Use RewriteCond !-f along with a rewrite to the desired output page and a flag of R=404.
You could use a PHP script for your 404 page:
ErrorDocument 404 /error404.php
There you can analyze the URL path with PHP (see $_SERVER['REQUEST_URI']) and determine what kind of resource has been requested or is expected.