+ All Categories
Home > Internet > Learn how to use the .htaccess file

Learn how to use the .htaccess file

Date post: 14-Jul-2015
Category:
Upload: istogram
View: 165 times
Download: 0 times
Share this document with a friend
25
Learn how to use the .htaccess file A quick and handy tutorial
Transcript

Learn how to use the .htaccess file

A quick and handy tutorial

December 2014 http://www.istogram.com 2

What is .htaccess ?

The .htaccess file is a configuration file for

web servers running the Apache Web

server software

December 2014 http://www.istogram.com 3

What is .htaccess ?

Configuration is performed by .htaccess

on a per-directory basis

December 2014 http://www.istogram.com 4

What is .htaccess ?

The dot before the filename means

that the file is hidden (in Unix-like

Operating Systems)

December 2014 http://www.istogram.com 5

Use .htaccess for :

● URL rewriting (pretty URLs, easy to remember URLs, SEF URLs)

December 2014 http://www.istogram.com 6

Use .htaccess for :

● URL redirects (site migrations, site upgrades, duplicate content)

December 2014 http://www.istogram.com 7

Use .htaccess for :

● Basic authentication

December 2014 http://www.istogram.com 8

Use .htaccess for :

● Security increase by blocking IP addresses

December 2014 http://www.istogram.com 9

Use .htaccess for :

● Enabling gzip compression to optimize performance

December 2014 http://www.istogram.com 10

Use .htaccess for :

● Creating custom error pages

December 2014 http://www.istogram.com 11

Use .htaccess for :

● Hiding directory listings

December 2014 http://www.istogram.com 12

Use .htaccess for :

● Enabling browser caching

December 2014 http://www.istogram.com 13

How to make a .htaccess:

● Use any text editor (such as NotePad, UltraEdit, TextEdit, Vim)

December 2014 http://www.istogram.com 14

Examples :

URL rewrite

RewriteEngine on RewriteRule ^new_fancy\.html$ old_ugly.html

# This directive allows us to type # /new_fancy.html and get the file # /old_ugly.html

December 2014 http://www.istogram.com 15

Examples :

URL redirect

REDIRECT DOMAIN.COM TO WWW.DOMAIN.COM

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

December 2014 http://www.istogram.com 16

Examples :

URL redirect

REDIRECT WWW.DOMAIN.COM TO DOMAIN.COM

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^domain.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

December 2014 http://www.istogram.com 17

Examples :

Simple URL redirect

Redirect 301 ^old\.html$ http://www.domain.com/new.html

December 2014 http://www.istogram.com 18

Examples :

Enable gzip compression

<ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule>

December 2014 http://www.istogram.com 19

Examples :

Password protection

AuthName "Member's Area Name" AuthUserFile /path/to/password/file/.htpasswd AuthType Basic require valid-user

.htpasswd : username:encryptedpassword fred_smith:oCF9Pam/MXJg2

Note that the password must be encrypted!

December 2014 http://www.istogram.com 20

Examples :

Custom error pages

ErrorDocument 401 /error_pages/401.html ErrorDocument 404 /error_pages/404.html ErrorDocument 500 /error_pages/500.html

December 2014 http://www.istogram.com 21

Examples :

Deny visitors by IP address

order allow,deny deny from 255.0.0.0 deny from 123.45.6. allow from all

Users from the IP address '255.0.0.0' as well as from the IP range '123.45.6.1' to '123.45.6.255' will be blocked.

December 2014 http://www.istogram.com 22

Examples :

Disable directory listings

IndexIgnore *

To prevent listing multiple file types :

IndexIgnore *.zip *.jpg *.gif

December 2014 http://www.istogram.com 23

Examples :

Leverage browser caching

<IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access 1 year" ExpiresByType image/jpeg "access 1 year" ExpiresByType image/gif "access 1 year" ExpiresByType image/png "access 1 year" ExpiresByType text/css "access 1 month" ExpiresByType text/html "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType text/x-javascript "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresByType image/x-icon "access 1 year" ExpiresDefault "access 1 month" </IfModule>

December 2014 http://www.istogram.com 24

Summary :

The .htaccess file can be a valuable tool for a

variety of tasks.

Learning how to use a .htaccess file is an

essential skill for any webmaster.

December 2014 http://www.istogram.com 25

Thank you!

You may also visit our blog at :

http://www.istogram.com/en/blog.html


Recommended