How to Change Folder Name in URL Using .htaccess


change folder name in url using.htaccess

When you move your web pages from one folder to another, you need to redirect them from their old locations to the new ones so that website visitors and search engine bots know that your content has moved. Apache allows you to accomplish this with the help of .htaccess file that you can use to modify web server configuration without accessing its config file. Let us take a look at how to change folder name in URL using .htaccess.

 

How to Change Folder Name in URL Using .htaccess

Here are the steps to change folder name in URL using .htaccess.

Before you proceed, please ensure that you have enabled .htaccess (mod_rewrite) in Apache. If not, then here’s how you can do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html)

 

1. Open .htaccess file

Open a terminal session or SSH into your Linux system. Open .htaccess file in a text editor


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

 

2. Change Folder Name in URL

Let’s say you run a website www.example.com and have a bunch of URLs in /apple/ folder. Let’s say you want to change them to /mobile/ folder. So when a user enters http://www.example.com/apple/iphone.php, they should be taken to http://www.example.com/mobile/iphone.php. In that case, add the following lines to your .htaccess file.

RewriteEngine on
RewriteRule ^apple/(.*) mobile/$1 [L,R=301] 

Change the 2 folder names above as per your requirement.

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

That’s it! Now when users enter URLs in /apple/ folder of your website, they will be redirected to /mobile/ folder and the URL on their web browser will also change accordingly.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!