Difference between revisions of "Redirecting site using apache configuration"

From Notes_Wiki
m
m
Line 4: Line 4:
There are many ways of reidrecting websites using apache. Most of them are discussed at http://www.yolinux.com/TUTORIALS/ApacheRedirect.html  The two important ways of redirecting websites among the ones mentioned at given URL are
There are many ways of reidrecting websites using apache. Most of them are discussed at http://www.yolinux.com/TUTORIALS/ApacheRedirect.html  The two important ways of redirecting websites among the ones mentioned at given URL are


=Using mod_alias=
==Using mod_alias==


<pre>
<pre>
Line 11: Line 11:




=Using mod_rewrite=
==Using mod_rewrite==


<pre>
<pre>
Line 21: Line 21:




=Redirecting based on IP address=
==Redirecting based on IP address==


<pre>
<pre>
Line 49: Line 49:




=Redirect using HTML META refresh=
==Redirect using HTML META refresh==


One simple way of redirecting is by sending META refresh message to browser, so that browser redirects to a different URL.  Browsers also cache the redirect HTML so this redirect is very effective and hardly generates any delay.  To achieve such redirects use HTML content similar to:
One simple way of redirecting is by sending META refresh message to browser, so that browser redirects to a different URL.  Browsers also cache the redirect HTML so this redirect is very effective and hardly generates any delay.  To achieve such redirects use HTML content similar to:

Revision as of 13:55, 20 February 2015

<yambe:breadcrumb self="Redirecting sites">Apache web server configuration</yambe:breadcrumb>

Redirecting site using apache configuration

There are many ways of reidrecting websites using apache. Most of them are discussed at http://www.yolinux.com/TUTORIALS/ApacheRedirect.html The two important ways of redirecting websites among the ones mentioned at given URL are

Using mod_alias

Redirect permanent / http://www.new-domain.com/


Using mod_rewrite

RewriteEngine On
RewriteRule /.* http://www.new-domain.com/ [R]

mod_rewrite is very powerful and can achieve powerful results. For detailed help on mod_rewrite visit https://httpd.apache.org/docs/current/rewrite/


Redirecting based on IP address

RewriteEngine On
RewriteCond %{REMOTE_ADDR} 193.189.116.235
RewriteRule !/block.* http://anusaaraka.iiit.ac.in/block/ [R]
RewriteCond %{REMOTE_ADDR} 79.186.193.82
RewriteRule !/block.* http://anusaaraka.iiit.ac.in/block/ [R]
RewriteCond %{REMOTE_ADDR} 10.2.59.228
RewriteRule !/block.* http://anusaaraka.iiit.ac.in/block/ [R]
RewriteCond %{REMOTE_ADDR} 46.22.173.101
RewriteRule !/block.* http://anusaaraka.iiit.ac.in/block/ [R]

Here if incoming request is from 193.189.116.235 or 79.186.193.82 etc. IPs and the request is not starting with /block, then the user is permanently redirected to /block. This kind of redirection can be used to block few very irritating IPs who might be using bots etc. for DOS attacks and when blocking the same using firewall is not desirable. Using this we can display a message from http://anusaaraka.iiit.ac.in/block, so that if any legitimate users get blocked by mistake, they know what to do to get unblocked.

Now an alias such as

 Alias /block /home/anusaaraka/block
 <Directory "/home/anusaaraka/block">
         Options All
         AllowOverride All
         Order allow,deny
         Allow from all
 </Directory>

can be added to serve blocked index.html from some other folder. This is useful when blocking attackers on servers were drupal, etc. CMS are installed and URLs get modified. Add these config as high in the httpd.conf as possible, so that it has precendence over other aliases and rules.


Redirect using HTML META refresh

One simple way of redirecting is by sending META refresh message to browser, so that browser redirects to a different URL. Browsers also cache the redirect HTML so this redirect is very effective and hardly generates any delay. To achieve such redirects use HTML content similar to:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
	<meta http-equiv="Refresh" content="0; URL=glpi" />
</head>
<body>
</body>
</html>

Here the URL can be changed from glpi to anything else. Both relative and absolute URLs work effectively.

<yambe:breadcrumb self="Redirecting sites">Apache web server configuration</yambe:breadcrumb>