Categories: Snippets

How To Set Up mod_rewrite for Apache on Ubuntu 18.04

Prerequisites:-

Make user Apache 2 installed on your server – How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04.

Step 1 — Installing Apache
sudo apt-get update
sudo apt-get install apache2
Step 2 — Enabling mod_rewrite
sudo a2enmod rewrite
sudo service apache2 restart
Step 3 — Setting Up .htaccess

In this section, we will setup a .htaccess file for simpler rewrite rule management. A .htaccess file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess is critical to your web application’s security. The period that precedes the filename ensures that the file is hidden. We will need to set up and secure a few more settings before we can begin. First, allow changes in the .htaccess file. Open the default Apache configuration file using nano or your favorite text editor.

sudo nano /etc/apache2/sites-enabled/000-default.conf

Inside that file, you will find the block on line 1. Inside of that block, add the following block:

# /etc/apache2/sites-available/default
<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    . . .
</VirtualHost>
Now, create the .htaccess file.
sudo nano /var/www/html/.htaccess

Add this first line at the top of the new file to activate the RewriteEngine.

/var/www/html/.htaccess
RewriteEngine on

Save and exit the file. To ensure that other users may only read your .htaccess, run the following command to update permissions.

sudo chmod 644 /var/www/html/.htaccess
Step 4 — Setting Up Files

In this section, we will set up a basic URL rewrite, which converts pretty URLs into actual paths to code. Specifically, we will allow users to access example.com/about. We will begin by creating a file named about.html.

sudo nano /var/www/html/about.html

Copy the following code into the HTML page.

# /var/www/html/about.html
<html>
    <head>
        <title>About Us</title>
    </head>
    <body>
        <h1>About Us</h1>
    </body>
</html>

You may access your web application at your_server_ip/about.html or example.com/about.html. Now notice that only about.html is accessible; if you try to access your_server_ip/about, you will get a Not Found error. We would like users to access about instead. Our rewrite rules will allow this very functionality.

sudo nano /var/www/html/.htaccess

After the first line, add the following.

/var/www/html/.htaccess
RewriteRule ^about$ about.html [NC]

 

Recent Posts

How To Troubleshoot Socket Errors in MySQL – Ubuntu

MySQL manages connections to the database server through the use of a socket file, a special…

5 years ago

How to remove .html, .php, etc (extension) from URL?

To remove the .html extension from your urls, you can use the following code in…

5 years ago

Ubuntu with Apache2: Installing and Configuring Your SSL Certificate

Use free/paid SSL certificates https://www.sslforfree.com Enable SSL Module Replace 'default-ssl' with the real site name…

5 years ago

Create zip archive excluding specific Files & Directories

Create archive of all files under public_html directory ignoring all files and folders including text…

6 years ago

how to use sudo command to install .tar.gz?

To install some file *.tar.gz, you basically would do: Open a console, and go to…

6 years ago

Change ownership of the directory in ubuntu

The chown command has the following syntax: [crayon-66370b818dc0b124473731/] So in your example command it is your primary…

6 years ago