Technology Bites

  • Home
  • Tech
    • Software
    • Browsers
    • Google
    • Internet
    • Windows
  • Mobile
    • Android
    • Apple
    • Windows Phone
  • Products
    • Phones
    • Tablets
  • TIP US
You are here: Home / Other / Useful .htaccess tips and tricks

Useful .htaccess tips and tricks

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”
[/shell]

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
[/shell]

Share this:

  • Tweet

Related

About Ram

I am a blogger and Technology Enthusiast. I write about software, tech news, gadgets. You can reach me at ram@teknobites.com, follow me on Google+ or on Twitter

« Facebook mobile apps redesign makes photos 3 times larger
Google Chrome for iPhone & iPad is coming? »

Trackbacks

  1. How to install PHP and APC on Ubuntu VPS • Technology Bites says:
    June 19, 2012 at 12:10 pm

    […] Dedicated VPS Server with Linode Installing Apache webserver on Ubuntu VPS Useful .htaccess tips and tricks […]

  2. Useful .htaccess tips and tricks • Technology Bites | Interesting Links says:
    August 23, 2012 at 4:34 pm

    […] Useful .htaccess tips and tricks • Technology Bites. Share this:TwitterFacebookStumbleUponTumblrPinterestLike this:LikeBe the first to like this. This entry was posted in Links. Bookmark the permalink. ← Documents show Samsung fixated on a ‘Beat Apple’ strategy, but did it cross the line? | The Verge […]

  3. Dedicated VPS Server with Linode • Technology Bites says:
    September 26, 2012 at 2:28 pm

    […] Installing Apache webserver on Ubuntu VPS How to install PHP and APC on Ubuntu VPS Useful .htaccess tips and tricks […]

Top Posts

  • Micromax A34 with 3.5-inch display is available for Rs. 4399
  • DSP Manager: The Best Equalizer app for Android
  • Download 10000 Free Shapes for Photoshop
  • Motorola, Indiegogo partner for new Moto Mods ideas
  • 40+ Free Windows Software alternatives
  • OnePlus 3T is launching on Dec 2 in India
  • Mibbit: IRC in Your Browser
  • 4 Handy Alternative Flickr Search Engines
  • MSN launches new home page
  • Flickr App Garden: Developer Showcase

About Us · Archive · Disclaimer & Privacy Policy · TIP US · Copyright © 2025 · Technology Bites