htaccess Redirect To Subfolder Without Changing URL

htaccess Redirect to Subfolder Without Changing URL
htaccess Redirect to Subfolder Without Changing URL

DISCLOSURE: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

If you have a habit of typing the URL of a site directly in the URL bar of your browser, you might’ve noticed that the browser sometimes redirects you to a different URL than the one you typed in. While doing so is helpful to direct your traffic to a specific page on your website, the process to do it is not so straightforward.

So if you’re looking to tinker with your htaccess file to redirect your alias URLs to a subfolder on your website, here’s how you can do it without changing your original URL.

What Is Htaccess And Why Do You Need It

The .htaccess (Hypertext Access) file on your website is used to configure your server without the need to go into each configuration file and change its settings manually. You can use this file to alter the behavior of your server, such as by adding passwords, creating custom error messages, or even redirecting your traffic to another page.

In simple words, htaccess basically consists of lines of code that can be added or removed to modify the current functionalities of your server. However, bear in mind that this file is only available on servers that use Apache (a web server software).

htaccess Redirect To Subfolder Without Changing URL

How To Access Htaccess File

If you try to look for the .htacess file on your server, you’ll have a tough time trying to find it since Apache makes sure it’s hidden. That’s because it holds the capability of making significant changes to your server, and if fallen into the wrong hands, can compromise your entire server and website.

However, the .htaccess files can be found in the public_html folder of your server. Here’s how you can access it:

  1. Log in to your hosting account’s control panel (also called Cpanel).
  2. Open File Manager, and on the navigation menu on the left screen of the screen, search and select the “public_html” folder.
  3. Inside, open the WordPress folder and then search for the .htaccess file. If you don’t see it, it’s probably because it’s hidden.
  4. If the file is hidden, you’ll need to head to Settings → Preferences → Show Hidden Files and uncheck the option.

How To Redirect To Subfolder Without Changing URL

So for instance, if you have a website called “Example” and you want people to go to the URL “www.example.com/store” when they type in “www.example.com”, how can you manage that? Well, we’re explaining exactly that below:

Closeup of Computer Screen With Address Bar of Web

Note: Keep in mind that you’ll need to enable an option that allows the Apache software to configure your server through the .htaccess file. If you’ve enabled it already, you can skip to Step 4

  1. Go to your terminal to enable mod_rewrite and type in $ sudo a2enmod rewrite. Then restart the server by typing in $ sudo systemctl restart apache2
  2. Apache doesn’t allow you to configure .htaccess at first. To allow that, type in $ sudo vi /etc/apache2/sites-available/000-default.conf and then before the </VirtualHost> line, add these lines of code:
    <Directory /home/455258.cloudwaysapps.com/zcpkukpzcg/public_html>
           Options Indexes FollowSymLinks MultiViews
           AllowOverride All
           Require all granted
    </Directory>

Now, restart the server by using the commands in Step 1

  1. Next, create the .htaccess file by typing in $ sudo vi /home/455258.cloudwaysapps.com/zcpkukpzcg/public_html/.htaccess in the terminal. Then add the following line on top of your .htaccess file: RewriteEngine on
  2. Finally, if you want to redirect your users from the old domain (www.example.com) to the newer domain (www.example.com/store) without changing the URL, add these lines anywhere in your .htaccess files:
    RewriteEngineOn
    RewriteCond%{HTTP_HOST} ^old-site.com RewriteRule ^(.*) http://new-site.com/$1 [P]

Then restart your server once again for the files to take effect.

Computer Keyboard With Word Restart on the White

Conclusion

While using htaccess to redirect your site to another subfolder without changing the URL isn’t that complicated of a task; however, if you haven’t done it before, we recommend not tinkering with the settings of the file. If you do feel adventurous, then back up your original htaccess file somewhere so you can always go back to it if things go wrong.