We face different scenarios where we need to redirect some specific sites to certain locations. There are two kind of redirects – Permanent (301) or Temporary (302) redirects. You can achieve these redirects based on your scenario by changing .htaccess file available in your root server. Here are collection of .htaccess redirects for different scenarios.
Redirect complete site to any other domain
#Redirect complete site to any other domain
Redirect 301 / http://www.example.com/
Redirect .html extensions to .php extensions
RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php
Redirect index.html to specific folder under root
# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/
Redirect visitors to WWW
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Redirect visitors to non WWW
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\example\.com$ [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Redirect Single Page to some other location
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Put https:// for every page on your site (Secured Version)
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Put https:// for pages in particular folder
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} somefolder RewriteRule ^(.*)$ https://www.example.com/somefolder/$1 [R,L]
Related: 9 Rules to have Search Engine Friendly Slug or Permalink for Posts