We have previously done a video on the .htaccess file for apache servers, and how is can help to normalize your URLs and in particular help re-direct any inbound links to the correct page.
We have since expanded on the htaccess file that we use with our sites and have added other commands that we believe help your website to perform in the search engines and improve usability.
The code includes, Rewrite conditions, Error documents, Cache Expiry and Compression.
The rewrite condition is designed to ensure your website has 1 URL per page indexed by the search bots, You can use the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.domain.co.uk/$1 [R=301,L]
The first part of the Rewrite condition re-directs the domain to include the www.
The second piece re-directs away from the /index.php or specific file name back to the root.
2.) Error Doc - Tells the server where to re-direct the user to if a dead link is found or a URL is not longer active.
ErrorDocument 404 /404.php
3.) Expires On - Tells a browser how long to hold the information of a web page in its cache, this improves the load time for rrevisiting users, this includes html, javascript and images.
ExpiresActive On
ExpiresDefault “access plus 7200 seconds”
4.) Compression (Gzip or Deflate) - You can use either Gzip or Deflate dependent on server capability, this example uses mod_deflate and firstly checks if the functionality is enabled then dependent of the browser, the files are compressed and minified to improve the load time of the site.
<ifmodule mod_deflate.c>
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</ifmodule>
Thanks for watching.