Apache web server is the most commenly used web server for hosting sites. You can configure apache using configuration file stored at /etc/apche2/apache2.conf or httpd.conf depending on your setup. Apache also allows configuration at the directory lvel in thr form of .htaccess files. This is quite useful as users won’t have access to httpd.conf file but they can modify the .htaccess files according to their need. The rules you place in the .htaccess file will override the httpd configuration file giving you more control over your site.
You can use htaccess files to many purposes including rewriting urls, adding error documents, and restricting access to certain files or directories and much more. Here are few tips that are helpful if you have a site or blog.
Set TimeZone
You can set the timezone for your server using htaccess.
[shell]SetEnv TZ Asia/Calcutta[/shell]
Set 301 Permanent Redirects
[shell]Redirect 301 /old.html http://www.example.com/new.html[/shell]
Remove WWW
There is a debate whether to keep www along with the domain name or remove it for SEO purpose. Adding www or skipping it won’t change anything, but it is always better to follow one rule for SEO benefits. If you prefer to skip www from your domain name use the below code.
[shell]RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301][/shell]
If you are among those who prefer www along with the domain name use this code.
[shell]RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301][/shell]
Rewrite URL
Create pretty permalinks for search engines from dynamic urls
[shell]RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /shop.php?cmd=$1&category=$2&product=$3 [L][/shell]
In this case the original URL is http//www.yourdomain.com/shop.php?cmd=product&category=gadgets&product=mobile, with the above rule it will be rewritten to http://www.yourdomain.com/product/gadgets/mobile.html
Hotlink protection
Hotlinking is bad for not only that the other site is stealing your images but in the process waste lot of your bandwidth, to get around this problem you can use these .htaccess rules to prevent that.
This will block images from being hotlinked from your site
[shell]RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L][/shell]
You can also show an image in place of the hotlinked image
[shell]RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ /images/hotlink.jpg [NC,R,L][/shell]
Skip the download dialogue
When you try to download files like pdf or doc you will get a request asking you to choose whether to save or open that file for you. To avoid that you can use the below code so the prompt will go directly to save as dialog.
[shell]AddType application/octet-stream .pdf .doc .ppt .xls .mov .mp3[/shell]
Change default index page of a directory
[shell]DirectoryIndex myindex.html[/shell]
Create a custom error page.
If you want to create custom error pages on Linux Apache server, you can use .htaccess to show pretty error documents. After creating the error documents you need to specify them in your .htaccess file. Don’t forget to set the path and filenames to reflect your server path and filename.
[shell]ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php[/shell]
If you don’t want to make error documents and just want to show a message, you can do that too.
[shell]ErrorDocument 401 Authentication Required
ErrorDocument 403 Forbidden
ErrorDocument 404 Not found
ErrorDocument 500 Internet server error[/shell]
Block certain IPs Using htaccess
You may want to block some IPs from accessing your site (for example referrer spam), you can block them by putting this code in your .htaccess.
[shell]allow from all
deny from 72.45.10.110[/shell]
Not only single IPs but you can block a range like this
[shell]deny from 72.47 [/shell]
Compress files
Compressing files will help in reducing the loading time for your site
[shell]# compress text, html, and other files
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript[/shell]
Prevent directory browsing.
You can prevent visitors to a directory which doesn’t have an index file with this rule.
[shell]Options All -Indexes[/shell]
Alternatively you can allow visitors to browse directory with no index file
[shell]Options All +Indexes[/shell]
Restrict file upload limits for PHP
Restrict the uploading file size in PHP, and set the maxiumum execution time for PHP scripts. See how to edit php.ini for increasing memory limit.
[shell]php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_execution_time 200
php_value max_input_time 200[/shell]
Set Cache-Control Headers
Set cache control header for different file types, set longer times for static files like images as they won’t change often.
[shell]# cache image, pdf files for 5 weeks
Header set Cache-Control “max-age=3024000, public”
#cache css, js, xml and text files for 2 days
Header set Cache-Control “max-age=172800”
# cache html and htm files for 2 hours
Header set Cache-Control “max-age=7200, must-revalidate”
Protecting files
You can use htaccess to protect specific files from accessing by others using the files directive, protecting .htaccess itself
[shell]
order deny,allow
deny from all
[…] Dedicated VPS Server with Linode Installing Apache webserver on Ubuntu VPS Useful .htaccess tips and tricks […]