Mod_rewrite module is used in Apache HTTP Server for rewriting URL based on the specified rules. Mod_rewrite relies on a rule-based rewriting engine which is based on a PCRE regular-expression parser. In order to configure the module in Apache, follow the steps below:
1. Enable mod\_rewrite:
In most cases, mod\_rewrite module is not enabled by default. You will need to enable it in Apache configuration file. You can enable it by using the following command in Terminal: \`\`\` sudo a2enmod rewrite \`\`\` It will enable the module, if not already enabled. After enabling the module, you need to restart the Apache service to make the changes effective. \`\`\` sudo service apache2 restart \`\`\`1. AllowOverride Directive:
To use mod\_rewrite from .htaccess files (which is common in applications like WordPress), you need to set the AllowOverride directive to FileInfo or All for your website directory. Open the Apache configuration file: \`\`\` sudo nano /etc/apache2/sites-available/000-default.conf \`\`\` Then find the directory for your website and add the following lines: \`\`\`1. Rewrite Rules :
Now you can add your rewrite rules in the .htaccess file. An example of a rewrite rule: \`\`\` RewriteEngine On RewriteRule ^article/(.\*)$ /article.php?id=$1 \`\`\` This rule will rewrite URLs like “example.com/article/42” to “example.com/article.php?id=42”.1. Restart Apache:
Now after configuring everything restart Apache again to make sure all changes take effect. \`\`\` sudo service apache2 restart \`\`\`That’s it, you are able to use mod\_rewrite for URL rewriting in Apache.
Note: Be sure to replace all paths and file names with what applies to your system and configuration.