How to Make a 301 Redirect with .htaccess
Redirect 301 is a way to permanently redirect from your old page to your new page or from your old site to your new site, and it is very important for your website to be search engine friendly.
With 301 redirect, you can notify search engines and other visitors that your website's address or the location of your page has changed permanently and ensure that your website has a search engine friendly structure.
.htaccess with 301 redirect rules
We'll list some common .htaccess 301 uses below.
Redirecting a single page on your website to another page or another website
Redirect 301 /old-page.html /new-page.html
Redirect 301 /old-page-2.html https://www.mysitename.com/new-page.html
With the example above, you can make 301 redirects to targets from different sources such as http or https.
Redirect an entire website to another website
It is the most commonly used and search engine friendly method to change the domain name of your website without disrupting the link structure of your website. It permanently redirects all the pages of your old website to your new website one-to-one.
Redirect 301 / https://mysitename.com
Forwarding your address without www to the address with www
If you want all links to your website to work with the www prefix, you can do so with the help of the following code.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysitename.com [NC]
RewriteRule ^(.*)$ http://www.mysitename.com/$1 [L,R=301,NC]
Thus, all your pages will redirect to the www version.
Forwarding your www address to an address without www
For those who want to use their web address without the www prefix, here is the solution. You can easily redirect your pages accessed with the www prefix to the www-less version permanently.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysitename.com [NC]
RewriteRule ^(.*)$ http://mysitename.com/$1 [L,R=301,NC]
So all your pages will be displayed without the www prefix.