htaccess apache server directives btm 395: internet programming

7
.htaccess Apache server directives BTM 395: Internet Programming

Upload: branden-boyd

Post on 05-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: htaccess Apache server directives BTM 395: Internet Programming

.htaccessApache server directives

BTM 395: Internet Programming

Page 2: htaccess Apache server directives BTM 395: Internet Programming

The .htaccess file

• Gives Apache various instructions for handling any HTTP request

• The full, exact name of the file is “.htaccess”– The initial dot indicates a hidden file in Linux– To handle this file in Windows/Mac you MUST set

your operating system to display file extensions• .htaccess directives (instructions) apply to the

folder where you place the file AND to all subfolders– If you add another .htaccess file in a subfolder, a

directive there overrides any directives from parent folders

• This file is very powerful! Be sure to make a backup of your original working .htaccess file before making any changes

Page 3: htaccess Apache server directives BTM 395: Internet Programming

Common .htaccess directives

• Rewriting and redirecting URLs• Custom error pages• Blocking visitors from viewing folder

contents• Password protection of folders

Page 4: htaccess Apache server directives BTM 395: Internet Programming

Rewriting vs. redirecting URLs

• URL rewriting– The visitor enters one URL, Apache internally

converts it to an internal address. E.g.• Visitor enters: http://wpsite.ca/category/old-post• Apache internally rewrites: http://wpsite.ca/?p=123• Visitor never knows what happened

– Lots of useful rewrite tricks• URL redirection

– The visitor enters one URL, Apache serves them back a new URL. E.g.

• Visitor enters: http://wpsite.ca/category/old-post• Apache serves: http://wpsite.ca/category/new-post-

name• Visitor gets new URL and can update it

Page 5: htaccess Apache server directives BTM 395: Internet Programming

Redirecting URLs

• Two main types– Code 301: signals a permanent URL change– Code 302 (the default): signals a temporary URL change

• Convenience shortcuts– E.g. Redirect /btm395 /bios/pro/teach/btm-395– Sends requests to http://chitu.okoli.org/btm395 to

http://chitu.okoli.org/bios/pro/teach/btm-395– Code 302 by default

• Changed URLs– Signals permanent changes in a URL– Use Code 301

• In WordPress, many plugins exist to handle redirection

Page 6: htaccess Apache server directives BTM 395: Internet Programming

Custom error pages

• The dreaded “Error 404 Page Not Found”• Specify custom error page in .htaccess

– ErrorDocument 404 /path/to/errordoc.html

• Custom error pages in WordPress– Default page is 404.php in the theme– Exact files might be different in your theme– To customize, copy relevant theme files to a

child theme and then customize them

Page 7: htaccess Apache server directives BTM 395: Internet Programming

Other .htaccess directives

• Blocking visitors from viewing folder contents– Options -Indexes

• Password protection of folders– Many web hosts have easier tools for

password protection• Many other .htaccess directives exist

– Simple yet Comprehensive guide to .htaccess

– Even more comprehensive but not so simple Ultimate guide to .htaccess directives