How to Redirect to Parent Folder in Apache

Sometimes you may need to redirect one or more URLs on your website to their parent folders, especially when you move around folders or restructure your website. You can easily do this using .htaccess (mod_rewrite) in Apache. In this article, we will look at how to redirect to parent folder in Apache server. This is useful if you need to move contents of a folder to its parent folder and redirect your old URLs to new ones.


How to Redirect to Parent Folder in Apache

Here are the steps to redirect to parent directory of a URL. Let us say you want to redirect all URLs in http://example.com/data/project to http://example.com/data


1. Enable mod_rewrite

First you need to enable mod_rewrite (.htaccess) for your Apache server. If you have already enabled it, then you can skip this step.

Otherwise, depending on your Linux distribution, run the following commands to enable it.

Ubuntu/Debian

Open terminal and run the following command to enable mod_rewrite.

$ sudo a2enmod rewrite

Redhat/Fedora/CentOS

Open Apache configuration file in a text editor.

$ sudo vi /etc/apache2/httpd.conf
OR
$ sudo vi /etc/httpd/httpd.conf

Look for the following line.

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.

Also look for the following Directory tag and change AllowOverride from None to All.

. . .
<Directory /var/www/html>
. . .
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
. . .
</Directory>
. . .


2. Redirect to Parent Folder

Create/Open .htaccess file in your website’s root folder.

$ sudo vi /var/www/html/.htaccess

Add the following lines to your .htaccess file.

RewriteEngine On
RedirectMatch 301 /project/(.*) http://example.com/data/$1

In the above code, the first line enabled mod_rewrite if it is not enabled. In the second line, Apache server will match any request that contains “project” and redirect them to /data/ folder along with the rest of query string.

You can modify the above statement as per your requirement.

If the above statement does not work for you, try the following which is a more specific.

RewriteEngine On
RedirectMatch 301 /data/project/(.*) http://example.com/data/$1


3. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

That’s it. Now if you open browser and visit URL starting with http://example/data/project/ (e.g. http://example.com/data/project/test.html) you will be automatically redirected to http://example.com/data (e.g. http://example.com/data/test.html) followed by requested URLs query string.

In this article, we have learnt how to redirect URLs to their parent directory. This is very useful when you have to move content from a folder to its parent folder/directory and have to redirect all old URLs to new ones.

Also read:

How to List Open Ports in Linux
How to Rewrite URL to Subdirectory in Apache
How to Rewrite URL with Parameters in Apache
How to Combine Two Files in Linux
How to Rewrite URL to Another URL in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *