301 redirects allow one URL to be permanently redirected to another URL. This is especially useful when the URL of a page or content on a website changes. The .htaccess file is a configuration file used on Apache servers and is often used to create these types of redirects. Here's an example of doing a 301 redirect in a .htaccess file:
Apache:
RewriteEngine on RewriteRule ^old-url$ /new-url [R=301,L]
In this example, a URL named old-url is 301 redirected to a URL named new-url. [R=301] indicates to Apache that this redirect is a 301 Permanent Redirect. [L] ensures that no other rule sets are checked after this rule is applied.
To add these rules to the .htaccess file, open the file in your text editor and make the necessary edits. Next, upload the .htaccess file to the root of the web server, or make sure it's already there if it exists.
There are a few points to note:
The .htaccess file must be created or edited directly with the text editor. Some text editors may replace such files with hidden characters, which may cause errors.
Apache's mod_rewrite module must be enabled. Usually, it is enabled by default on most servers, but you can check the server configuration files (usually the httpd.conf file) to be sure.
To avoid making errors in configuration, be careful when modifying the .htaccess file. Incorrect configuration may result in website downtime or errors.
You can create 301 redirects in .htaccess file by following these steps.