htaccess apache server directives btm 395: internet programming
TRANSCRIPT
![Page 1: htaccess Apache server directives BTM 395: Internet Programming](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/1.jpg)
.htaccessApache server directives
BTM 395: Internet Programming
![Page 2: htaccess Apache server directives BTM 395: Internet Programming](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082417/56649f315503460f94c4d262/html5/thumbnails/7.jpg)
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