Search code examples
apache.htaccessmod-rewriteconfigurationforward

Redirect traffic from folder or its subfolders to another folder


I've got an Apache Webserver running on the default var/www/html path.

This looks like the following:

var (Folder)
|   www (Folder)
|   |   html (Folder)
|   |   |   index.php (File)
|   |   |   some_other_files.php (File)
|   |   |   ab (Folder)
|   |   |   |   index.php
|   |   my_app (Folder)
|   |   |   ab.php (file)
|   |   |   login (Folder)
|   |   |   |   index.php
|   |   |   index.php (File)

Now I want to redirect all the traffic from domain.com/my_app and its subfolders to the actual var/www/my_app directory.

This should not just redirect the var/www/html/index.php file to var/www/my_app/index.php, but also all the traffic from the subfolders of html should be sent to the folders in my_app accordingly.

Examples:

domain.com/my_app/       => var/www/my_app/index.php
domain.com/              => var/www/html/index.php
domain.com/abc           => var/www/html/abc/index.php
domain.com/my_app/login/ => var/www/my_app/login/index.php
domain.com/my_app/ab.php => var/www/my_app/ab.php

Note that this should FORWARD / REWRITE the requests to the folders, not just REDIRECT them!

How can I achieve this either by implementing the propper .htaccess or editing the apache conf file?

Thank you in advance!


Solution

  • I finally found the solution:

    <VirtualHost *:80>
        DocumentRoot /var/www/html/
    
        Alias /my_app "/var/www/my_app/"
        Alias /my_app/ "/var/www/my_app/"
        <Directory "/var/www/my_app/">
            Order allow,deny
            Allow from all
            AllowOverride All
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    This way, all the HTTP traffic to domain.com/my_app gets sent to /var/www/my_app/ and its subfolders accordingly!