How to Enable Apache Mod_Rewrite?
Last Updated :
20 Jun, 2024
Mod_rewrite stands as a pivotal Apache module known for its robust URL manipulation capabilities. It empowers webmasters to rewrite URLs, a crucial feature widely utilized in content management systems such as WordPress. This module excels at transforming complex, dynamic URLs into cleaner, more user-friendly formats. By enhancing readability and structure, mod_rewrite contributes significantly to the overall user experience of websites. In this guide, we will delve into the essential steps for enabling mod_rewrite on an Apache Server running Ubuntu 18.04 VPS.
Prerequisites
- Ubuntu 18.04 VPS
- A non-root user with sudo privileges
- Apache web server
Step 1: Enable mod_rewrite
You can enable any Apache module using the a2enmod command. So run the command below on your Ubuntu 18.04 server:
$ sudo a2enmod rewrite
Your server is now ready to accept rewrite rules.
Step 2: Setup your server to accept .htaccess files
- You can set up URL rewrite rules directly in Apache’s configuration file, but it's better to use the `.htaccess` file for each website. Most CMSs rely on `.htaccess`, which is created by default during installation. To enable `.htaccess`, you must edit each website's virtual host file and add the following code:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
- For instance, you can edit the default virtual hosts that ships with Apache using a nano editor by typing the command below:
$ sudo nano /etc/apache2/sites-available/000-default.conf
- Then copy paste the above text just before the ‘</VirtualHost>’ closing tag. Remember to save the file by pressing CTRL + X then Y and Enter
- Then, restart Apache for the changes to take effect:
$ sudo systemctl restart apache2
Step 3: Mod-rewrite syntax
The basic Apache mod_rewrite syntax has the following parts:
RewriteRule pattern substitution [flags]
- RewriteRule – the directive of our rule.
- Pattern – this is a regex (Regular Expression) that matches what the user types in a browser.
- Substitution – The actual URL path that should be called by the Apache server.
- Flag – optional parameters that modify how the rules work.
Step 4: Create a sample .htaccess file
- We will now create a sample ‘.htaccess’ file at the root of the default website to test mod_rewrite. To do this type the command below:
$ sudo nano /var/www/html/.htaccess
- Every mod_rewrite rules must be with the commands ‘RewriteEngine on’. So you need to type this at the top of the file.
RewriteEngine on
- Next, we are going to rewrite a rule that redirects users to a ‘contact_us.html’ page if they request the URL http://ipaddress/contact_us
- So we add the below rule:
RewriteRule ^contact_us$ contact_us.html [NC]
- In the rule above, 'contact_us' is matched and redirected to 'contact_us.html'. The '[NC]' flag makes it case insensitive. '^' matches text after the domain/IP, and '$' signifies the end of the URL. The complete `.htaccess` file should look like this:
RewriteEngine on
RewriteRule ^contact_us$ contact_us.html [NC]
- Save the file by pressing CTRL+ X, Y, and Enter.
- Next type the command below to create the contact_us.html page:
$ sudo nano /var/www/html/contact_us.html
Then, paste the HTML text below on the file:
HTML
<html>
<head>
<title>Contact our website</title>
</head>
<body>
<h1>This is a contact us page</h1>
</body>
</html>
- Save the file by pressing CTRL+ X, Y and Enter.
- Now if you visit the path http://ipaddress/contact_us on a browser, Apache should serve you with the page ‘contact_us.html’.
Similar Reads
How to Enable mod_rewrite for Apache 2.2? Enabling mod_rewrite in Apache 2.2 on a Windows system involves a few simple steps. mod_rewrite is a powerful Apache module used for URL rewriting, which helps create cleaner, more SEO-friendly URLs. Here is a detailed guide on how to enable it on Windows. PrerequisitesApache installedAdministrator
3 min read
How To Enable mod_ssl in Apache? The mod_ssl module in Apache allows you to secure the web server with SSL or TLS encryption. This article will show how to enable mod_ssl in apache to ensure the your website handles secure HTTPS connections. PrerequisitesApache HTTP Server.Administrative privileges on the Windows system.A valid SSL
3 min read
How to check if mod_rewrite is enabled in PHP ? In PHP, there is an inbuilt function called âphpinfoâ. By using this function, we can output all the Loaded Modules and see the âmod_rewriteâ is enabled or not. Syntax: phpinfo(); Here is a process to check the âmod_rewriteâ load module is enabled or not. Note: The local server used here is XAMPP. C
1 min read
How to Enable or Disable Apache Modules? Apache, one of the most widely used web servers, is known for its flexibility and power. This flexibility is largely due to its modular architecture, which allows administrators to enable or disable specific functionalities as needed. Apache modules can extend the core functionality of the server to
3 min read
How to Debug Apache RewriteRule or mod_rewrite? The mod_rewrite module in Apache is used to rewrite URLs. This is a powerful tool that allows the client to modify the requested URL before the server processes the request. Rewrite rules are defined in Apache configuration files (e.g., httpd.conf) or .htaccess files. The mod_rewrite usesRewriting U
3 min read
How to Enable CORS in Apache Web Server? Cross-Origin Resource Sharing(CORS) is a security feature that allows web browsers to make requests to a different domain than the one serving the web page. without CORS, browsers restrict such requests due to security concerns. Enabling CORS ensures that your web server responds correctly to cross-
2 min read
How To Enable or Disable CGI Scripts in Apache? This article will guide you on how to enable or disable CGI scripts in Apache. Configuring CGI scripts in Apache is a crucial aspect of managing dynamic content generation on web servers. The Common Gateway Interface (CGI) provides a standardized protocol for executing programs, allowing websites to
4 min read
How to Enable & Set Up .htaccess File on Apache? The .htaccess is a simple but extremely powerful configuration file used by the web servers running on apache web server software. this .htaccess file allow to alter and change their configuration of the main configuration files without even having direct access to them. In this guide, we will look
3 min read
How to Hide Apache Server Signatures? When you visit a website, the server sends back information about itself, including a signature that can be exploited by attackers. To enhance website security, you can hide this signature. For Apache servers, adjust the "ServerSignature" and "ServerTokens" settings in the configuration file to disa
4 min read
How to Disable HTTP Methods in Apache? To prevent the collection of specific system calls that could offer attackers Windows running Apache servers a backdoor, we can Turn off unwanted HTTP methods on the Apache web server. This increases the security of our web application and prevents unwanted attacks. PrerequisitesApache InstalledAdmi
2 min read