Redirect calls to index.php to root in Apache

Redirect all calls to index.php to the root of your domain. This can be particularly useful when considering the SEO impact of having two identical pages, and perhaps links linking to one or the other, therefore spreading the backlink benefits.

Enter the following in your htaccess file.


RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

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

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"

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"

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"

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