Testing and Troubleshooting .htaccess in Apache: A Dummy Project Guide

If you're working with Apache on your Ubuntu system and want to ensure that your .htaccess file is configured correctly, a dummy project can be a handy tool for testing. This step-by-step guide assumes a basic understanding of Apache and its installation on an Ubuntu system.


1. Create a Dummy Project


Start by creating a directory for your dummy project:


sudo mkdir /var/www/html/rewrite-htaccess.local

Inside this directory, create an `index.html` file and add some content to it:

sudo nano /var/www/html/rewrite-htaccess.local/index.html

Add the following content to `index.html`:


html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Apache .htaccess Test</title>

</head>

<body>

    <h1>Welcome to the .htaccess test page</h1>

</body>

</html>



2. Create .htaccess file


Inside the project directory, create a `.htaccess` file:

sudo nano /var/www/html/rewrite-htaccess.local/.htaccess


Add the following content to `.htaccess`:


RewriteEngine On

RewriteRule ^test$ index.html [L]



This rule will rewrite requests to `/test` to the `index.html` file.


3. Create Apache VirtualHost File


Create a virtual host configuration file:



sudo nano /etc/apache2/sites-available/rewrite-htaccess.local.conf


Add the following configuration:


<VirtualHost *:80>

    ServerAdmin webmaster@rewrite-htaccess.local

    ServerName rewrite-htaccess.local

    DocumentRoot /var/www/html/rewrite-htaccess.local


    <Directory /var/www/html/rewrite-htaccess.local/>

        Options Indexes FollowSymLinks

        AllowOverride All

        Require all granted

    </Directory>


    ErrorLog ${APACHE_LOG_DIR}/rewrite-htaccess-error.log

    CustomLog ${APACHE_LOG_DIR}/rewrite-htaccess-access.log combined

</VirtualHost>



Save and exit the file.


4. Enable the VirtualHost


Enable the virtual host:


sudo a2ensite rewrite-htaccess.local.conf



5. Restart Apache


Restart Apache to apply the changes:


sudo systemctl restart apache2



6. Check the .htaccess


Open your browser and visit `http://rewrite-htaccess.local/test`. If the setup is correct, you should see the content from `index.html`.


7. Check Logs


Monitor the access and error logs in real-time:


tail -f /var/log/apache/rewrite-htaccess-access.log

tail -f /var/log/apache/rewrite-htaccess-error.log



This allows you to troubleshoot any issues by examining the logs for relevant information.


Congratulations! You've set up a dummy project to test the functionality of your .htaccess file. Adjustments can be made based on your specific requirements and configurations. Happy coding!