Methods of payment Abuse

Enable Apache Mod Rewrite

06.03.2022, 00:06

When a user enters a specific URL in a browser and presses Enter, the web server that receives the request tries to find a file on the server at the path specified in the URL. If nothing is found along the path, an index file such as index.html or index.php is opened. if nothing is found, a 404 error is returned. We wouldn't have beautiful and easy to understand URLs if things didn't work the way they do now. These URLs are used on many sites. The apache mod rewrite module is designed to solve this problem In this tutorial we will see how to enable it.

Enabling the mod

If everything worked as described, then when opening any link of this site in the root directory should exist a file or script with the same name from the address bar. But it doesn't. When requesting this URL, the web server makes attempts to find such a file, but if it does not find it, instead of returning a 404 error, it passes control to the mod_rewrite module, which for all such URLs executes the index.php script, passing it the query string after the domain. PHP then uses this data to find and return the page the user needs.

To enable mod rewrite run a simple command:

$ sudo a2enmod rewrite

After restarting the web server:

$ sudo systemctl restart apache

Just because the module is enabled at the Apache web server level does not mean that it will work for a website. For this purpose, it must be configured in the .htaccess file, specify on which script to pass requests to non-existent pages. In order for the .htaccess file to work, the AllowOwerride: All directive must be added to the Directory section of the virtual host

As an example:

Next, for example, in WordPress you need to add lines like this to the .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

All code is placed in the IfModule directive, allowing the code inside it to be executed only when the mod_rewrite module is enabled, otherwise these lines are simply ignored. The RewriteEngine On directive enables the module for the current directory. Then RewriteBase specifies that the entire string after the domain should be passed to the script. Next come RwriteRule rules with conditions for them RewriteCond, which is executed strictly sequentially.

One of the RewriteRule rules ^index .php$ - [L] says that if the URL contains index.php, the URL should be rewritten to /. In this regular expression, the entire string is specified and the dot is escaped with a backslash. The [L] flag means that the URL matches this rule, then the following rules should not be checked. After this rule is executed, the URL will be rewritten and the web server will assume that it has received the request /, the rule analysis will start over and this time it will match the last rule.

The RewriteCond conditions apply to all subsequent rules. So we have RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d allow the last rule to be executed only if the URL is not a file or folder. The final rule redirects everything to the ./index.php script.

If you can't understand the mod_rewrite settings, it's worth looking at what's going on inside the web server during your redirects. To do this in the configuration of the virtual host of the site, you need to add this line.

For example:

$ sudo vi /etc/apache2/sites-available/001-texts.conf

LogLevel warn rewrite:trace4

Then in the log file, which is given in the ErrorLog directive, you will see all the attempts of the web server to convert the URL according to your rules and will be able to understand what you are doing wrong. In this tutorial, we went over how to enable Apache mod rewrite, as well as how it all works and how to look for errors.