What is .htaccess in PHP?

.htaccess is a configuration file for web servers running the Apache server software. When a .htaccess file is placed in a directory that is loaded via the Apache web server, the file is detected and executed by Apache.

.htaccess files can be utilized to modify the setup of the Apache server software to enable additional functionality and features. We can use the .htaccess file for various configuration alterations in Apache web server software. Some common uses are listed below −

ErrorDocuments

Creating custom error pages allows us to show website visitors a friendly error message when a URL on your website does not work ?

ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
ErrorDocument 403 /error_pages/403.html

Password Protection

We can easily password protect a directory 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 second line indicates the location of the password file. The third line determines the authentication type (Basic HTTP authentication). The fourth line indicates that we require valid login credentials.

Redirection

Redirects enable us to direct website visitors from one document to another ?

Redirect /old_dir/ http://www.example.com/new_dir/index.html
Redirect 301 /old-page.html /new-page.html

Deny Visitors by IP Address

We can block specific IP addresses while allowing all others ?

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 IP addresses '155.0.2.0' and '123.45.6.1' while allowing all other IP addresses.

Adding MIME Types

To set up a MIME type, create a .htaccess file with the following text ?

AddType text/html .htm0
AddType application/pdf .pdf
AddType image/svg+xml .svg

'AddType' determines that you are including a MIME type. The second part is the MIME type (such as text/html), and the last part is the file extension (such as '.htm0').

URL Rewriting

One of the most powerful features of .htaccess is URL rewriting using mod_rewrite ?

RewriteEngine On
RewriteRule ^product/([0-9]+)/?$ /product.php?id=$1 [L]
RewriteRule ^user/([a-zA-Z0-9]+)/?$ /profile.php?username=$1 [L]

Note: .htaccess files require Apache web server with appropriate modules enabled (mod_rewrite, mod_auth, etc.). Place the .htaccess file in the directory you want to affect, and it will apply to that directory and all subdirectories.

Conclusion

.htaccess is a powerful tool for configuring Apache server behavior without accessing the main server configuration. It enables custom error pages, password protection, redirects, IP blocking, MIME types, and URL rewriting to enhance website functionality and security.

Updated on: 2026-03-15T08:15:43+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements