How does a .htaccess file work in PHP and what is its scope in terms of directories?

A .htaccess file in PHP is used to configure server settings and permissions for a specific directory or website. It allows you to control access to certain files, set up URL redirections, and enable additional features such as compression and caching. The scope of a .htaccess file is limited to the directory it is placed in and any subdirectories within it.

# Example of a .htaccess file in PHP
# Deny access to specific file types
<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
  Order Allow,Deny
  Deny from all
</FilesMatch>

# Enable gzip compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml
</IfModule>

# Redirect to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]