A URL redirect is one of the most common uses of the .htaccess file. It is used when the URL of a document has changed. Redirects allow redirecting the visitors of your website from one specific document to another.
It’s useful, for instance, if your website content has been moved and you want to redirect your visitors from the old links that new location. This is helpful if you have changed the domain name or reorganized your website.
For setting up redirects, you need to make a .htaccess file by following the guidance and the instructions including the text below.
Redirect /old_dir/ http://www.yourdomainname.com/new_dir/index.html
The codes above will instruct your Apache Web Server to display the ‘index.html’ document located in ‘new_dir’ directory if there is a visitors’ request to display ‘old_dir’ directory.
Here in the above example, you can see that ‘old_dir’ is the document location that the visitor can request, and it is a directory or document located in your actual domain. So, in the above example, the ‘old_dir’ directory to be located in ‘https://www.yourdomainname.com/old_dir/’. You’ll also notice that the file location where the visitors are redirected is a complete website URL and isn’t a relative URL for case of ‘old_dir’. It means that the visitors can be redirected to the folder ‘old_dir’ to any website document, it doesn’t necessarily need to be there in your website content and it could be any website.
It’s important for you to know the differences between a full/absolute URL and a relative URL as it’s one of the most common causes of error too. A full or absolute URL includes the complete domain name. Relative URL is the document location within the website, it doesn’t include the website domain name.
An example of a full/absolute URL would be ‘http://www.yourdomainname.com/directory/file.html’. And an example of relative URL would be ‘/directory/file.html’.