How to configure Apache rewrite rule-based request URL on Ubuntu 22.04

To Configure Apache Rewrite Rule Based Request URL On Ubuntu 22.04

Intoduction

The Apache HTTP Server, frequently referred to as Apache, is a highly effective and versatile web server software utilized for delivering web content. A key feature of Apache is its capability to manage URLs and handle requests through rewrite rules facilitated by the mod_rewrite module. This module provides a powerful means to dynamically alter request URLs based on defined patterns, resulting in cleaner URLs, an enhanced user experience, and improved search engine optimization (SEO).

Procedure:

Step – 1: Check the OS version

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step – 2: Check the status of the Apache2 service by using the following command

root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-09-11 04:27:08 IST; 9min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 2826 (apache2)
      Tasks: 55 (limit: 4556)
     Memory: 5.3M
        CPU: 87ms
     CGroup: /system.slice/apache2.service
             ├─2826 /usr/sbin/apache2 -k start
             ├─2831 /usr/sbin/apache2 -k start
             └─2832 /usr/sbin/apache2 -k start
Sep 11 04:27:08 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Sep 11 04:27:08 linuxhelp apachectl[2822]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1>
Sep 11 04:27:08 linuxhelp systemd[1]: Started The Apache HTTP Server.

Step – 3: Change directory to Apache document root directory

root@linuxhelp:~# cd /var/www/

Step – 4: List the directory

root@linuxhelp:/var/www# ll
total 12
drwxr-xr-x  3 root root 4096 Sep 11 04:27 ./
drwxr-xr-x 15 root root 4096 Sep 11 04:27 ../
drwxr-xr-x  2 root root 4096 Sep 11 04:27 html/

Step – 5: Now create two directories

root@linuxhelp:/var/www# mkdir old-page
root@linuxhelp:/var/www# mkdir new-page

Step – 6: Then create one HTML file in each directories

root@linuxhelp:/var/www# touch old-page/old.html
root@linuxhelp:/var/www# touch new-page/new.html

Step – 7: Add some text to the each files.

root@linuxhelp:/var/www# echo "Old HTML File" >> old-page/old.html 
root@linuxhelp:/var/www# echo "New HTML File" >> new-page/new.html 

Step – 8: Now open default virtual host file and change the path

root@linuxhelp:/var/www# vim /etc/apache2/sites-available/000-default.conf 
DocumentRoot /var/www/
<Directory /var/www/>
		AllowOverride All
		Require all granted
	</Directory>

Step – 9: Restart the Apache2 service by using following command

root@linuxhelp:/var/www# systemctl restart apache2

Step – 10: Open the browser and search the IP with old file path

Step – 11: Now again go to the terminal and create .htaccess file to create rewrite rule by using following command.

root@linuxhelp:/var/www# vim .htaccess
RewriteEngine On
RewriteRule ^old-page/(.*)$ /var/www/new-page/$1 [L]

Step – 12: Enable Apache rewrite module by using following command.

root@linuxhelp:/var/www# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

Step – 13: Restart Apache2 service by using following command

root@linuxhelp:/var/www# systemctl restart apache2

Step – 14: Go to the browser and remove the old.html file then reload the page. It will show the new.html file.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to configure Apache rewrite rule based request URL on Ubuntu 22.04. Your feedback is much welcome.

FAQ
Q
How do I internally rewrite a URL to a different path?
A
To internally rewrite a URL without changing the visible URL:
RewriteEngine On
RewriteRule ^old-path/(.*)$ /new-path/$1 [L]
• This rewrites old-path/something to new-path/something internally.
Q
How do I redirect an old URL to a new URL?
A
To redirect an old URL to a new URL:
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
• R=301 specifies a permanent redirect.
• L means this is the last rule to be processed if the rule matches.
Q
What is the basic syntax of a rewrite rule?
A
The basic syntax of a rewrite rule is:
RewriteRule Pattern Substitution [Flags]
• Pattern: The regular expression to match the incoming URL.
• Substitution: The URL to which the request should be rewritten or redirected.
• Flags: Optional parameters to control the behavior of the rule (e.g., [L] for "last rule").
Q
How do I enable mod_rewrite?
A
To enable mod_rewrite:
• Open the Apache configuration file (httpd.conf or apache2.conf).
• Ensure the line LoadModule rewrite_module modules/mod_rewrite.so is uncommented.
• Restart Apache to apply the changes (sudo systemctl restart apache2 or sudo service httpd restart).
Q
What are Apache rewrite rules?
A
Apache rewrite rules are configurations the Apache HTTP Server uses to modify or redirect incoming request URLs. They are defined in the .htaccess file or the server configuration files (e.g., httpd.conf) using the mod_rewrite module. These rules enable URL manipulation, redirection, and rewriting