What is the apache2 .htaccess File

The .htaccess file is a configuration file used by the Apache web server to manage settings on a per-directory basis. It allows you to override global settings without modifying the main server configuration file. Common uses for .htaccess include:

  • URL Rewriting: Redirecting URLs or creating cleaner, more readable URLs using the RewriteRule directive.
  • Access Control: Restricting access to specific files or directories with directives like Require or Deny.
  • Custom Error Pages: Specifying custom pages for error responses (e.g., 404 Not Found) using the ErrorDocument directive.
  • Caching: Controlling caching behavior for resources with Expires or Cache-Control headers.
  • Security: Enhancing security by blocking specific IP addresses or preventing directory listing.

To use .htaccess, simply create the file in the desired directory and add the necessary directives. Make sure that the Apache server is configured to allow .htaccess overrides with the AllowOverride directive in the main configuration file.

The .htaccess that I use:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # Remove index.php from URL
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Explained

  • <IfModule mod_rewrite.c> : Rewrite rules will only be applied if the mod_rewrite module is loaded and enabled.
  • RewriteEngine On : Enable the rewrite engine. The RewriteEngine allows you to create complex URL manipulations, such as rewriting URLs for cleaner web addresses, redirecting users, or controlling access based on specific conditions.
  • The RewriteBase directive in an .htaccess file is used in conjunction with mod_rewrite to specify the base URL for rewriting rules.
  • RewriteCond %{REQUEST_FILENAME} !-f : If the requested path is not a directory, it rewrites the request to index.php, passing the original path as a query parameter.
  • RewriteCond %{REQUEST_FILENAME} !-d : If the requested resource is not a directory, the request will be rewritten to index.php, passing the original request path as a query parameter.
  • RewriteRule ^(.*)$ index.php/$1 [L] : Here’s a breakdown of the rule:

  • ^(.*)$ : This matches the entire requested URL path. The .* captures any characters (except for line breaks), and the parentheses create a capturing group.
  • index.php/$1 : This indicates that the request should be rewritten to index.php, followed by the content captured by the first group (i.e., whatever was matched by (.*)).
  • [L]: This flag tells Apache to stop processing any further rewrite rules if this rule matches. It effectively means “this is the last rule to be processed.”

  • </IfModule> :Closes the check if mod_rewrite is loaded.

I use this .htaccess because I am planning on creating a simple Mode-View-Controller framework.

This particular configuration requires the Apache2 mod_rewrite be installed and enabled.

Use the PHP function $_SERVER[‘REQUEST_URI’] to collect the URI as a string that can be processed by the PHP code.

Given a URL of https://www.example.com/user/add/ – by using $_SERVER[‘REQUEST_URI’] in the index.php file the $_SERVER[‘REQUEST_URI’] will return a string “/user/add/”.

In M-V-C this would translate into load and run the class user. Then run the method/function add. Basically the code would show a form to create a new user.

Conclusion

In this article we cover that the .htaccess file is a configuration file used by the Apache web server to manage settings on a per-directory basis. It allows you to override global settings without modifying the main server configuration file. I show the .htaccess file I use, with explanations, for my homegrown Model-View-Controller framework that grabs the URI using the PHP $_SERVER[‘REQUEST_URI’] function.