What is mod_rewrite?

Mod_rewrite is an Apache web server module. It does not have to be installed or enabled by default. Mod_rewrite manipulates browser-submitted URLs. It translates them to deliver content to the browser. The module disguises how and from where content is serving.
It increases site security. It also benefits sites that utilize search engines, since search engines work best with static URLs. Mod_rewrite removes the dynamic portion of the URL (eg: index.php ... ), so it converts to static format, thus preventing potential conflicts and increasing site visibility.

Turn on mod_rewrite

sudo a2enmod rewrite

What is .htaccess?

.htaccess is a configuration file of Apache Web Server. When a .htaccess file is placed in a directory, it will be loaded by the Apache Web Server. When the .htaccess file is detected and executed by Apache, it will alter the web server configuration by enabling/disabling additional functionality. These modifications can be, e.g., redirections. For instance, if a 404 file not found error occurs

Set .htaccess for the most common situations

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
linux, apache2, configs
comments powered by Disqus