1. Introduction
.htaccess is server config file that's used on Apache Web Server. Apache Web Server will read .htaccess in your web folder and execute it. These .htaccess can be used to modify config of Apache Web Server. Example that will on/off features and addition features that Apache Web Server has to offer. Addition features include such as redirect to another page when [404 Page not found]
2. Server Install
Purpose you use Xampp to install Apache server as below
+ Path: D:\Xampp
+ Config file: D:\Xampp\apache\conf\httpd.conf
+ Vhost file: D:\Xampp\apache\conf\extra\httpd-vhosts.conf
3. Project
+ Location: D:\Projects\test
+ Mapping Virtual host: D:\Xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost test.local:80>
DocumentRoot "D:/Projects/test"
<Directory D:/Projects/test/>
AllowOverride none
</Directory>
</VirtualHost>
4. Permit change config apache in project
Permit change config Apache Web Server in D:\Projects\test
+ Mapping Virtual host: D:\Xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost test.local:80>
DocumentRoot "D:/Projects/test"
<Directory D:/Projects/test/>
AllowOverride All
</Directory>
</VirtualHost>
+ After you setting AllowOverride All Apache Server will read all file .htaccess in D:\Projects\test to modify server config
5. Example you have project D:\Projects\test\Htaccess\PageNotFound
+ Domain: http://test.local/Htaccess/PageNotFound
+ .htaccess file: D:\Projects\test\Htaccess\PageNotFound\.htaccess
<IfModule mod_rewrite.c>
</IfModule>
6. Redirect to notfound.html when you have 404 page
+ Use ErrorDocument
+ Source Tree
D:\Projects\test\Htaccess\PageNotFound
|__.htaccess
|__index.php
|__notfound.html
+ .htaccess
<IfModule mod_rewrite.c>
ErrorDocument 404 /Htaccess/PageNotFound/notfound.html
</IfModule>
7. Read .acl instead of .htaccess
+ Config at virtual host
+ Mapping Virtual host: D:\Xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost test.local:80>
DocumentRoot "D:/Projects/test"
AccessFileName .acl
<Directory D:/Projects/test/>
AllowOverride All
</Directory>
</VirtualHost>
+ Source Tree
D:\Projects\test\Htaccess\ChangeHtaccessFile
|__.acl
|__index.php
8. Options: Permit you read content of folder
+ Source Tree
D:\Projects\test\Htaccess\ReadContentFolder\
|__.htaccess
|__index.php
|__folder
|__hello.txt
+ Case1: Don't permit read content in folder .htaccess
<IfModule mod_rewrite.c>
</IfModule>
+ Case2: Permit read content in folder .htaccess
<IfModule mod_rewrite.c>
Options Indexes
</IfModule>
Source Demo: https://github.com/quocvo87/Htaccess