How to configure redirection using .htaccess file. Htaccess is file which is used to make changes to your web server configuration without editing the server configuration file. This .htaccess file will be placed in your website root directory.
In this guide you are going to learn to how to configure some list of redirections which is mostly needed for your SEO and ranking in search engines.
These are the list of redirections you are going to learn here.
Table of Contents
Note: The below redirections use 301 permanent redirection, so you if need to can try the rules by using 302 temporary redirection instead and test them.
Redirect HTTP to HTTPS with www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
Redirect HTTP to HTTPS without www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
3. Redirect one page to another page
Syntax is
# Syntax
Redirect type(302/301) old_location new_location
# Redirect to a page
Redirect 301 /old_page https://www.domain.com/new_page
# Redirect to folder
Redirect 301 /old_page https://www.domain.com/folder/
Redirect old domain to new domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/" [R=301,L]
Redirect old domain to new domain while preserving the path
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/newdomain\.com\/$1" [R=301,L]
Redirect files location to new domain with same path
RewriteRule ^.*\.(pdf|doc)$ https://newdomain.com%{REQUEST_URI} [R=301,L]
Redirect except certain path
For example if you wish to redirect all your website frontend to a new domain except your WordPress wp-admin pages, then you need to use the following rules before the redirection rules.
RewriteCond %{REQUEST_URI} !^(.*)?wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)?wp-admin$
RewriteCond %{REQUEST_URI} !^/(wp-includes/.*|wp-admin/.*|wp-content/.*)$
Redirect subdomain to subfolder
This mainly used for redirecting a subdomain based site to a subfolder based site.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ subfolder
RewriteRule ^(.*) https://domain.com/%{REQUEST_URI} [R=301,NC]
RewriteRule ^(.*) https://domain.com/blog/%{REQUEST_URI} [R=301,NC]
hi, which code okay to use?
redirect from several specific subdomains then you can modify the above rule:
RewriteCond %{HTTP_HOST} ^dev\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev0\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev1\.example\.com [NC]
RewriteRule ^directory/ https://example.com%{REQUEST_URI} [R=302,L]
or this one:
RewriteCond %{HTTP_HOST} ^dev\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev0\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev1\.example\.com [NC]
RewriteRule ^directory/ https://example.com%{REQUEST_URI} [R=301,L]
which one to use?