Horje
How to Enable Apache Mod_Rewrite?

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’.



Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Make Apache HTTPD Service Start Automatically on Server Reboot and Startup? How to Make Apache HTTPD Service Start Automatically on Server Reboot and Startup?
How to Hide Apache Server Signatures? How to Hide Apache Server Signatures?
How to Add Menu in WordPress? How to Add Menu in WordPress?
How to Enable or Disable Apache Modules? How to Enable or Disable Apache Modules?
How to Change Author in WordPress Post? How to Change Author in WordPress Post?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16