Rewrite folder name in URL internally using htaccess

I had images stored with a folder, which was named descriptively. Unfortunately this meant that the resulting URL was larger than it had to be. Rather than rename the directoy, and make all existing image calls invalid, I wanted to allow the folder to be called by an alias in the URL. If the URL contains /a/example.jpg, it will rewrite to the actual directory name on my server, eg /a-very-long-directory-name/example.jpg. I entered this in my htaccess file.

RewriteEngine on
RewriteRule ^a/(.*) a-very-long-directory-name/$1

How to use mod_deflate to compress files in Apache

Despite the increase in home internet speeds, the time taken to transfer a file to a browser is often the slowest link in the chain. One way to reduce that time is to compress(zip) the file before sending it. You could gzip the file, or use deflate, like I do in the example below. Both are almost the same, but many argue that deflate is faster. You do not want to compress anything that is already compressed, eg jpg,mp3,png. Nothing would crash if you tried to compress an already compressed file, but you would be wasting CPU time. You could probably add any rss feeds to the list below, but I do not know the mime type of the top of my head. In the example mod_deflate must be loaded for the files to be compressed. You can add this into your .conf file, or into a .htaccess file.

<IfModule mod_deflate.c>
# compress text, html, javascript and css files
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Further reading is available on the apache mod_deflate page.

Cache Control using htaccess file

When a user visits a webpage, which contains an image, the browser will first check if it has the image in it’s cache. If it does, it will ask your web server if it has a newer version. If there isn’t a newer version, then the browser doesn’t download the image again, and instead displays it using its cached version. That’s very nice, because you do not have to send the image to the user again, but the web server has had to do a little work by checking the last time the file was updated. You can stop the browser asking so frequently by using a cache control header. This basically tells the browser not to ask if there is an updated version until a certain amount of time has passed. One way of setting this cache control up is using a .htaccess file. This assumes that you are using Apache.

The following example tells the browser to cache any gif, jpg, or png files for 1 month:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
</IfModule>

You could extend that to cache any CSS or javascript files by adding the following:

<IfModule mod_expires.c>
ExpiresByType text/javascript "modification plus 1 months"
ExpiresByType application/javascript "modification plus 1 months"
ExpiresByType text/css "modification plus 1 months"
</IfModule>

Alternatively, you could use the following, which caches anything with the listed extension for 1 day

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=86400"
</FilesMatch>

I personally use the final example, because it’s easier to list the file extensions, rather than listing the mimetypes.

Starting, stopping, or restarting Apache

Starting or stopping Apache in Linux (CentOS)

service httpd start
service httpd stop
service httpd restart

Restarting Apache should only take a second or so if everything goes well. I would be surprised if any of your users noticed that you did it. In saying that, don’t do it unless you have spare time to deal with a failure to restart. You might have made a mistake in one of the config files and that can stop Apache starting again. I am speaking from experience, where I have forgotten which change I made and spent at least ten minutes finding the syntax error.