What is .htaccess in PHP?

PHPProgrammingServer Side Programming

.htaccess is a configuration file for use on web servers running on the web apache server software. when a .htaccess file is placed in a directory which in turn loaded via the Apache web server, then the .htaccess file detected and executed by the Apache server software. 

.htaccess files can be utilized to modify the setup of the Apache server software to empower additional functionality and fetures that the apache web server softwatre brings to the table. We can use the .htaccess file for alteration various configuration in apache web server software. Some of them are listed below:

ErrorDocuments

Creating custom error pages is very useful, it allows us to show web site visitors a friendly error message, in case of if a URL on your web site does not work.

ErrorDocument 404 /error_pages/404.html

Password protection

Very easily, we can password protect a directory of an application that requires a username and password to access.

AuthName "Admin Area"
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user

The first line tells the Apache Web Server the secure directory is called 'Admin Area', this will be displayed when the pop-up login prompt appears. The subsequent line indicates the location of the password file. The third line determines the authentication type, in this example, we are using 'Basic' because we are using basic HTTP authentication lastly the fourth line indicates that we require valid login credentials

Redirection

Redirects enable us to direct web site visitors from one document within your web site to another.

Redirect /old_dir/ http://www.test.com(your domain)/new_dir/index.html

Deny visitors by IP address

order allow,deny
deny from 155.0.2.0
deny from 123.45.6.1
allow from all

The above lines tell the Apache Web Server to block visitors from the IP address '155.0.2.0' and '123.45.6.1' and allow all other IP addresses.

Adding MIME types

To set up a MIME type, create a .htaccess file following the main instructions and guidance which includes the following text:

AddType text/html htm0

'AddType' determines that you are including a MIME type. TThe subsequent part is the MIME type, for this situation content or HTML, and the last part is the file extension, in this example 'htm0'.

raja
Published on 28-Jun-2019 19:14:08
Advertisements