An HTACCESS is a file with some configuration information used by Apache Web Servers. It is used to control access to the resources of the server from the outside. Let us learn techniques of how to forward URLs from one protocol to another and one prefix to another using HTACCESS Redirect technique. This file is useful to modify the website for SEO benefits.
Top 6 HTACCESS Redirect URL Techniques
HTACCESS is used to restrict access to Files, URLs, Rewrite URLs, configure Error pages, enabling GZIP or compression and more. An HTACCESS file name contains only a DOT and htaccess extension. SEO (Search Engine Optimization) experts insist on maintaining a single URL for a single Resource or Page.
Servers treat webpages differently that are with or without PREFIX (WWW) and the Protocol (HTTP/HTTPS). You should forward the other versions to the required version of URL so that Search Engines like Bing, Google, DuckDuckGo and others land on a single resource with a single URL.
In one of the two possible Redirects 301 and 302, we extensively use the 301 Redirect for SEO benefit. Redirect 301 indicates "Moved Permanently" and the redirect 302 indicates "Moved Temporarily". Remember that 301 redirects pass the Link Juice from one URL to another URL. These URLs can be on the same or different domains.
All these redirections are kept under an IF condition that checks whether the Rewrite Module(mod_rewrite.c) is ON or OFF. Find this IF condition inside an already existing ".HTACCESS" file and add the code. No need to put separate IF condition again.
Here is the list of 6 popular redirection types.
- HTTP to HTTPS
- HTTPS to HTTP
- Non-WWW to WWW
- WWW to Non-WWW
- PageA to PageB
- 404 to ErrorPage
Use these below ready-made HTACCESS Redirect Generators to make your redirection job easy.
1. Redirect HTTP to HTTPS Type
Google started giving more value to HTTPS sites. HTTPS offers end to end encryption between the server and the browser. You should set up a Redirect from HTTP to HTTPS after installing the SSL certificate. You can either check for HTTPS ON/OFF condition or HTTPS 443 Port status. You can directly Copy-Paste the below template.
<IfModule mod_rewrite.c> RewriteEngine On #RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule>
2. Redirect HTTPS to HTTP Type
Redirecting from HTTPS to HTTP is not recommended for a normal website. To reduce the burden on servers, some developers prefer switching the HTTPS traffic to HTTP. If you do not depend on Search Engines to pick your site URLs, you can do anything. You can directly Copy-Paste the below template.
<IfModule mod_rewrite.c> RewriteEngine On #RewriteCond %{SERVER_PORT} ^443$ RewriteCond %{HTTPS} on RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule>
3. Redirect Non-WWW to WWW URL Type
CMS (Content Management Systems) software prompts developers for the first time to choose from Prefixed WWW and Non-WWW versions. Famous CRMs are Wordpress, Joomla and Drupal. Maintaining a single version of URL is good in SEO point of view.
Replace the SITE URLs "abc.com" and "http://www.abc.com" with your working site URLs.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^abc.com$ RewriteRule (.*) http://www.abc.com/$1 [R=301,L] </IfModule>
4. Redirect WWW to Non-WWW URL Type
Advantage of Non-WWW version of URL in SEO point of view is that you will gain more Backlinks to your site over a period of time. People do not want to type the Prefix WWW every time. Backlink auditing tools like MOZ shows different results in the absence of WWW.
Replace the SITE URLs "www.abc.com" and "http://abc.com" with your working URLs.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^www.abc.com$ RewriteRule (.*) http://abc.com/$1 [R=301,L] </IfModule>
5. Redirect Page A to Page B Vice Versa
If you want to put a Forward Rule from one page to the another, you should include the relative path in the .htaccess file. It means, both the URLs are on the same working domain. A leading Forward-Slash is mandatory.
Replace "page-a" and "page-b" with your site pages.
<IfModule mod_rewrite.c> RewriteEngine On Redirect 301 /page-a /Page-b </IfModule>
6. 404 (Page Not Found) to Custom Error Page
A custom error page for 404 Page Found Errors looks good for visitors. It easily informs users that something is wrong with the URL or the address.
Replace the Page URL "www.abc.com/errpage" with your Error Page URL.
<IfModule mod_rewrite.c> RewriteEngine On ErrorDocument 404 www.abc.com/errpage </IfModule>
There are many types of HTACCESS Redirects or Configurations which are useful to maintain fine-grained controls over the resources of the Server.
It is time to Share this Tutorial with your friends and colleagues to encourage authors.
Other Interesting articles from us: