Gzipped javascript not being served properly - javascript

I have a js file and a gzipped version of this js file.
The problem I am facing is trying to serve the gzipped version of this js file to the browsers that support it.
I don't know how to do that. If I add the js.gz to the current script element then it is not loaded and gives error.
How can I automatically serve the gzipped version of this js to the supported browsers.
Also I would like to restrict the gz serving the the folder /resources/widget/
I don't want to compress on the fly as i have around 1000 requests per second and it would take minutes to take down the server. Each js file is about 100KB and js.gz is about 16KB, so I would appreciate if I could be helped with my current files.

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} “.*Safari.*” [OR]
RewriteCond %{HTTP:Accept-Encoding} !gzip
RewriteRule (.*)\.jgz$ $1\.js [L]
AddType “text/javascript;charset=UTF-8″ .jgz
AddEncoding gzip .jgz
rename your files from .gz to .jgz
OR use
<FilesMatch "\\.js.gz$">
ForceType text/javascript
Header set Content-Encoding: gzip
</FilesMatch>
<FilesMatch "\\.js$">
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript
</FilesMatch>

I have encountered problems with some browsers (notably Safari) not dealing appropriately when the file name extension was ".gz". We had to work around this by renaming the files to ".jgz"

You can use httacces to gzip for you, to browsers that support it:
see:
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

if you're using php :
In your htaccess file :
RewriteEngine On
RewriteRule (.*)\.js compress-js.php?file=$1.js [L]
compress-js.php :
ob_start("ob_gzhandler");
header("Content-type: text/javascript; charset: ISO-8859-1");
echo (file_get_contents ($file));
That's all you need your files will be served in a compressed format.
Just add these 2 files .htaccess and compress-js.php to your js folder or wherever you store them.

Related

.htaccess blocks images from mobile site aka root/mobile

I been researching countless topics regarding accessing images when a user is logged into a mobile site. I'll try to explain everything to the point and any help is super appreciated.
I have website (root/index.html) and a mobile version redirected to (root/mobile/index.html) via .htaccess
I have all my subscribers individual folder accounts as followed: (root/1234567, root/1122334, root/9876543, etc..) with their uploaded images inside (root/1234567/image1.png, etc...)
When a user logs into mobile via (root/mobile/login.php) and tries to view an image via JavaScript www.mysite.com/1234567/image1.png it "breaks".
I tried all suggestions in my research, however when I delete .htaccess from root it works so I know the root .htaccess file is the culprit but I do need security on subscribers folders (root/1234567 etc...) but allow access on image files within subsribers folders. I can post both .htaccess files contents of root and root/mobile
Here's my root .htaccess file
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_USER_AGENT}
"android|blackberry|iphone|ipod|ipad|iemobile|opera
mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^((?!mobile/).*)$ /mobile [L,R=301]
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak|txt)$">
Order Allow,Deny
Deny from all
</FilesMatch>
ErrorDocument 403 https://www.mysupersite.com/
ErrorDocument 404 https://www.mysupersite.com/
Options -Indexes
And my root/mobile .htaccess file
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.mysupersite.com/mobile [L,R=301]
RewriteCond %{HTTP_USER_AGENT}
"!android|blackberry|iphone|ipod|ipad|iemobile|opera
mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^ https://www.mysupersite.com [L,R=301]
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak|txt)$">
Order Allow,Deny
Deny from all
</FilesMatch>
ErrorDocument 403 https://www.mysupersite.com/mobile
ErrorDocument 404 https://www.mysupersite.com/mobile
Options -Indexes

remove the index.php WAMP

I have a site and it works to type "www.mysite.com/folder/" (if i have a index file in that folder).
It also works to type "www.mysite.com/folder/index.php"
All fine and good, but I want it so if you type /index.php it will remove the index.php on the end also if I use a form and post to index.php I don't want that to show in the url.
I have looked around and seen every solution say .htaccess rewrite, but It don't seem to be working / or its just not the same thing that I want?
or do I need to use javascript and update the address bar?
(I'm using WAMP)
EDIT:
Things I've have tried:
1
httpd.conf
AllowOverride All
2
I have enabled:
Apache modules> rewrite_module
3
I have created a .htaccess placed in my wamp/www folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
4
wamp/apps/phpmyadmin/config.inc.php
added
$cfg['index_page'] = '';
EDIT 2:
This look like it works, but it messes up my POST to index.php so it breaks my login (It don't start the session).
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
An .htaccess file is definitely the correct solution. If it isn't working, try one of these possible answer:
.htaccess not working on localhost with XAMPP
How to use .htaccess in WAMP Server?
.htaccess not working on WAMP
Update: Re-reading your question, you might be using an older htaccess script (for PHP < 5.2.6 version). I'm not sure if the script will work in current versions. Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
The ? after index.php in final line is the difference
If above doesn't help, what version of PHP does the PHP command phpinfo() report?
<?php
phpinfo();
die();

Mod_Rewrite variable dynamic directory inside of two static ones

So I am trying to force javascript files to reload whenever we as developers update the files. In order to do this we are using the answer found here https://stackoverflow.com/a/7671705/805779 and we think this will work great however I am not sure how to do the mod rewrite. So here is the link I have
www.mysite.com/js/v1/myjs.js
I need to change it to
www.nysite.com/js/myjs.js
This is what I have so far but it is not working
<IfModule mod_rewrite>
RewriteEngine On
RewriteBase /
RewriteRule ^/js/(.*)$/(.*)$ js/$2 [L,NC,R]
</IfModule>
After some more testing I found my main issue was not adding .c to my IfModule, with that in mind the following is "partially" working
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/css/(.*)/(.*)$ /css/$2 [L,NC,R]
RewriteRule ^/js/(.*)/(.*)$ /js/$2 [L,NC,R]
</IfModule>
This works for my classic example
www.mysite.com/js/v1/myjs.js => www.nysite.com/js/myjs.js
However this does not work if more then one variable are involved. aka
www.mysite.com/js/v1/min/pages/myjs.js
My version number v1 will always be directly after the root folder so js/v1 or css/v1 and I want to remove the v1 and keep everything after it
You can use this single rule for this redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?((?:css|js))/v\d+/(.+)$ /$1/$2 [L,NC,R]
</IfModule>

How to hide .html extension in url using Javascript

Is there any way to hide .html extension from URL using JavaScript.
FOR ex:
mysite.html hide.html here from url
You cannot achieve this through javascript, this need to be done through .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
More information about mod_rewrite module here.
You can with html5, but also you can use url rewriting, or seo friendly urls, on the server side.
window.history.pushState({"html":'<html></html>',"pageTitle":'this is my title'},"", 'http://stackoverflow.com/mynewurl');
However keep in mind you can only change the url to one which is the same domain as the original!
There is a great library called history.js which helps with cross browser and version compatibility

Lost css/js after setting on gzip comression

So I decided to try gzip on my website and finally found script fully accteptable by Google PageSpeed Insights which increase my score to 86/100 from 72/100. The only problem is that all of my gzipped jsess and cssess disappear and the website looks like there is no .css file at all. I honestly have no idea why this isn't working correctly.
Here's the code I put it in .htaccess
RewriteEngine On
RewriteBase /
<IfModule mod_headers.c>
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
<FilesMatch "(\.js\.gz|\.css\.gz)$">
Header set Content-Encoding gzip
Header append Vary Accept-Encoding
</FilesMatch>
Thanks in advance for any help

Categories