Redirection Using .htaccess file

Here are some simplest rules to set some redirections using the .htaccess file.

 

How to write rewrite rule (URL rewriting, mod_rewrite)

1. To redirect a site from http to https :

Add the below in .htaccess file in public_html

===================================================

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

===================================================

 

2. Redirecting a domain to another domain via .htaccess

Example : Redirect abc.com to google.com

===================================================

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc\.com$ [OR]

RewriteCond %{HTTP_HOST} ^www\.abc\.com$

RewriteRule ^/?$ “http\:\/\/www\.google\.com\/” [R=301,L]

===================================================

3. To redirect users to access the site with WWW

example :- redirect abc.com to http://www.abc.com

Add the below in .htaccess file

===================================================

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc\.com$ [NC]

RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]

===================================================

 

4. Redirect page to another page within public_html

Example 1:- to redirect design.html to index.php

===================================================

RewriteEngine on

RewriteRule ^design.html$ index.php

===================================================

 

Example2 :- rewrite site abc.com/kb/index.php to abc.com/blog/index.html

Go to kb directory and create a .htaccess file using the following commands:

#cd public_html/kb

#touch .htaccess

#vi .htaccess

Then paste the following:

===================================================

RewriteEngine on

RewriteRule ^index.php$ /blog/index.html

===================================================

  • htaccess redirection
  • 2 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

How To Install and Use Docker on Ubuntu 16.04

Introduction Docker is an application that makes it simple and easy to run application processes...

Change Main IP of the server :: Vesta Panel

If we are changing the main IP of a server installed with Vesta panel, we can use the following...

cPanel: Apache-FastCGI Data Timeout Error

If you are using a cPanel server and have FastCGI enabled in Apache, you might be facing the...

Domainkey installer in cPanel

You can use the following cPanel script to install domainkey for an account...

Email issues :: Common Troubleshooting Steps

Here I will discuss some common errors and their solution: We are using a mail relay setup for...