The .htaccess file is a configuration file used by Apache-based web servers that provides a way to make configuration changes on a per-directory basis. One of the common uses of .htaccess is to redirect users from one page to another. Here’s how you can do this:
1. Locate or create a .htaccess file: The .htaccess file should be located in the root directory of your website. If you can’t find it, you can create a .htaccess file using a plain text editor.
1. Edit or add redirect code: When redirecting using .htaccess, there are a few different types of redirects you can use such as 301 (permanent), 302 (temporary), and others. Here are examples of how to do this:
- 301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most commonly used and powerful redirect as it passes nearly all of the link juice of the existing domain.
`Redirect 301 / http://www.newurl.com/`- 302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page or site and plan to switch back to your main site at a later date.
`Redirect 302 / http://www.newurl.com/`- Redirect a single page:
`Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html`1. Save your changes: After adding the code, save changes and ensure the .htaccess is uploaded to the web server. If it asks you if you want to overwrite the existing .htaccess file, choose ‘yes’ if the file exists.
1. Test your URL: Enter your old URL that should be redirected. If everything was done correctly, you should see that your browser redirects you to the new page you specified in your .htaccess file.
Remember that each directive in .htaccess needs to be on one line, so even the rules that appear on a line by themselves must be followed by a line break. Also, be cautious while working with .htaccess file, incorrect commands might create big problems leading to internal server errors.