Posts Tagged .htaccess
Keeping your sites optimised
Posted by admin in Web design & development on June 11, 2009
The sites I’ve been working on had been suffering from flab. A JavaScript library here (jQuery of course), a plug-in there, another, another, another… then a large plug-in that loads it’s own plug-ins (Shadowbox v3 – I’m talking to you).
It took a kick from outside (“your site x sucks HTTP requests and bandwidth”) to realise things were going downhill.
So – with the help of the good folk at Yahoo YSlow and the new copy-cat-kid on the block (Google Page Speed) with their plug-ins for a plug-in(firebug) for Firefox [oh the irony]… I’ve started to get things in line again.
Both YSlow and Page Speed have helpful links and information once called – and now I can pat myself on the back for having improved my YSlow scores from an ‘F’ to a ‘B’. Wetware heh.
Main issues I was able were too fix were too many HTTP requests and a lack of explicit caching direction from the web server. Nextly I’ll be looking at getting interface elements into a sprites for future jobs. (Must take this sprite/CSS creation tool for a spin. Looks absolutely fab.)
Reduce HTTP requests
While combining CSS files is trivial, combining a bunch of disparate JS files into one is not painless – I’ve had to drop a jQuery tooltip plug-in as it wouldn’t play nice when bundled up with its playground compatriots.
Server side caching and compression
is achieved the following htaccess rules:
##Cacheing static components FileETag none Header unset Etag # Far future 1 YEAR <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4|css|js)$"> Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT" </FilesMatch> <FilesMatch "scripts.inc.php$"> Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT" </FilesMatch> # Past last modified <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4|css|js)$"> Header set last-modified "Sun, 13 Jul 2008 20:00:00 GMT" </FilesMatch> <FilesMatch "scripts.inc.php$"> Header set last-modified "Sun, 13 Jul 2008 20:00:00 GMT" </FilesMatch> #Compress <IfModule mod_deflate.c> <FilesMatch "\.(js|css)$"> SetOutputFilter DEFLATE </FilesMatch> </IfModule>
Shadowbox woes
I now may be on the lookout for a replacement to Shadowbox. Shadowbow v2 was great – it came with lots of build options and could be explicitly launched by jQuery on Document Ready. v2 wasn’t IE8 compatible however – forcing an upgrade to v3 – still in beta… Ho hum. This required an updated version of the JW FLV media player which I’m not mad keen on – and that needs a new licence. While Shadowbox is now very clever and can automatically find it’s own dependencies – it has many of these – each one with an HTTP request. While it is easy to combine the whole bundle into a single file its something I’d rather not have to do/keep track of – I’ve already got a rod for my back with the other jQuery plugins…
Display proper maintenance page with .htaccess
Posted by admin in Web design & development on June 8, 2009
Create a decent “sorry this site is down for maintenace” page with a “have a Nice day” message then when you do need to upgrade things use the following htaccess rewrite rule to forward all traffic to this page.
Adapted from Marco’s Webdev Notepad
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^83\.57\.178\.37$
RewriteRule !maintenance/index.html /maintenance/index.html [L,NC,R=302]
From Ask Apache (I like)
ErrorDocument 403 http://www.sub.domain.com/ Order deny,allow Deny from all Allow from 83.57.178.37
Tiny URL?
Posted by admin in Web design & development on April 14, 2009
Zeldman’s just posted about the current mini-storm about URL shortening services…
I love his understated way. While the debate about these services – and, as an adjunct, feed scraping, we should look to our webdev monkey skills to roll our own solutions. He, yer Zeldman, doesn’t state as much in B&W but his example shows his own tiny URL. How? The simple redirect.
Whether or not this is done with a mod_rewrite rule or by any means necessary [dreams of CMS modules in my mind] – the fact is that it is easy a pie to make your own Tiny URLs and publish them hand in hand with the original article. Every article should have a permalink and a short version.
Of course – the redirect solution presupposes your domain isn’t www.pneumonoultramicroscopicsilicovolcanoconiosis.com…
.htaccess tips and tricks
Posted by admin in Web design & development on October 24, 2008
Rewrite
We *heart* canonical URIs
[Note - I've recently come accross a web hosting company that disallows Options +FollowSymLinks. When used their server throws an internal error page - you may need to replace with the more secure (on shared hosting systems anyway) Options +SymLinksIfOwnerMatch ]
Get rid of those pesky Ws.
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]
Or as you may well prefer – reinstate them
The SEO boys, they say “www“.
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
…rewrite continued:
#redirect all .html pages to .php
RewriteBase /
RewriteRule ^(.*).html$ $1.php [R=301]
Redirects
#specific page redirects - PERMANENT
redirect 301 /example-page.html http://example.com/new-name.html
#specific page redirects- TEMPORARY
redirect 302 /example-page.html http://example.com/new-name.html
Spell checker
Thanks to Jens Meiert This little line will allow a single typo in your users’ URIs (guess who just discovered that “URL” is a depreciated term):
CheckSpelling On
Get rid of any instances of “index.html”. And more
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*index.(html?|php) HTTP/
RewriteRule ^(([^/]+/)*)index.(html?|php)$ http://example.com/$1 [R=301,L]
Add trailing slashes where they may have been missed…
Thanks to Webmaster World
# If final URL-path-part does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.[^/]*|/)$
# Redirect to add a trailing slash
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
more…
Set PDFs to download rather than open in browser
#option for PDFs to download rather than open
AddType application/octet-stream .pdf
Disable directory browsing
# disable directory browsing
Options All -Indexes
Redirect denied directory listings etc to standard error page
# redirect denied directory listings etc to standard error page
ErrorDocument 403 /404-redirect.html
ErrorDocument 404 /404-redirect.html
Ease versioning of CSS files (+)
Will also improve history viewing with GitX
Note: this assumes a naming convention of, for example /css/filename.1234.css. Other dots will break this fragile little fellow.
#Rules for Versioned Static Files
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]