Apache .htaccess redirect

Redirecting with htaccess

To redirect using a .htaccess file, follow these steps:

1. Ensure you have access to the .htaccess file on your server. This file is usually located in the main directory of your website.
2. If the .htaccess file does not exist, you can create it using a text editor such as Notepad (for Windows) or TextEdit (for Mac). Make sure to save it as .htaccess without any file extension.
3. Before making changes to the .htaccess file, we recommend backing up the original version so you can restore it if needed.

To redirect traffic from one URL to another, use the following code in your .htaccess file:

Example of redirecting from www to non-www (i.e., from https://www.example.com to https://example.com):

ruby
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Example of redirecting from non-www to www (i.e., from https://example.com to https://www.example.com):

ruby
RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

In the above code examples, replace “example.com” with your own domain.

5. When you have made the necessary changes to the .htaccess file, save it and upload it to your server.
6. Ensure the redirect is working correctly by entering the new URL in your browser and checking if you are automatically redirected to the new address.
Note that changes to the .htaccess file can affect the functionality of your website, so always exercise caution when making these modifications.

See also  Modern website