Different methods of Redirecting a web page using Apache web server configuration file

Tutorials Apache

Redirecting web page using Apache is a common way of implementing redirection via setting some rules on the .htaccess file for the Apache Web Server.

Here at Fixwebnode, as part of our Website Support Services, we regularly help our customers to fix website errors and Apache related errors.

In this context, we shall look into the different ways in which Redirection is implemented using Apache HTTP Web Server.

 

How to Redirect a web page using Apache?

With the .htaccess file, you can easily make some changes which will allow redirection of web pages. The redirection can be implemented using different methods as will be shown below.

 

1. Using mod_alias to implement Redirection

With the mod_alias, URL manipulation tasks can be handled in a simple way. This gives room for the implementation of redirection from one URL to another by means of "Redirect" and "RedirectMatch" directives.

i. Using Redirect Directive

Redirect directive on Apache gives an opportunity to implement redirection on a simple and a one-page redirects. It links a connection between an Old URL to a new one by requesting from the client to get the resource again at a new location. This involves two arguments named, the old URL and the new URL.

You can simply add the following lines in your configuration file to effect a redirect;

<VirtualHost *:80>
ServerName www.your_domain1.com
Redirect / http://www.your_domain2.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.your_domain2.com
. . .
. . .
</VirtualHost>

What is happening here is that you are telling the browser to direct all requests for "www.your_domain1.com" to go to "www.your_domain2.com". This is meant for a single page and not not reflects on the entire website as earlier started.

Note that a "no status argument" was used in the directives making the redirect temporary (HTTP status 302). This signifies a temporarily moved resource. To implement a permanent redirect, you can use either of the following directive lines;

Redirect 301 /oldlocation http://www.your_domain2.com/newlocation
Redirect permanent /oldlocation http://www.your_domain2.com/newlocation

ii. Using RedirectMatch Directive

To redirect multiple pages, the RedirectMatch directive will allow you to implement this. 

With the RedirectMatch directive, patterns in parenthesis is matched with text in the redirect using "$1". "1" here represents the first group of text. Then other groups are given numbers sequentially.

For instance, lets say we want to match each request for something with a directory such as "/newDirectory" to a subdomain named "newDirectory.website.com", you can apply the following directive;

RedirectMatch 301 /blog(.*) http://www.newDirectory.website.com$1

What this will do is to redirect the newDirectory of the old website to the newDirectory of a new one.

 

2. Redirection implemented with mod_rewrite directive

To implement redirection on more complicated tasks such as manipulating the query string, the power of "mod_rewrite" module can be introduced. This involves the use of a rule based rewriting engine to rewrite the requested URLs. You should note that by default, the URL is mapped to a path in the filesystem. This allows for a redirection of a URL to another.

Therefore the mod_rewrite module allows for a limitless rules with rule conditions attached. These relies on environment variables, server variables, HTTP headers and time stamps.

To implement mod_rewrite, ensure that the following line is added as the first line in your .htaccess file;

RewriteEngine on

The mod_rewrite module involves "RewriteRule" and "RewriteCond" directives.

i. Using RewriteRule Directive

You can use the following directive to specify rules for the rewriting engine;

RewriteRule [pattern] [substitution] [flags]

Here, "Pattern" represents the actual URL by using regular expressions.

"substitution" translates that the actual URL of the page should be displayed containing the information that needs to be seen.

"flags" represents optional fields which can be at the end of the "RewriteRule" directory.

 

ii. Using RewriteCond Directive

Using RewriteCond Directive will help to specify a condition which will be taken with the rewriting action. To implement this, use the following directive as follows;

RewriteCond [test string] [condition] [flags]

The "Test string" represents a server variable with a format "%{VARIABLE NAME}".

"Condition" can be a regular expression which is a string comparison or a file/path test.

"Flags" as earlier stated are optional.

 

Need support in fixing Apache related Errors? We are available to help you today.

 


Conclusion

How to implement of one page to another via redirects on Apache by modifying .htaccess file? Apache redirect can help to redirect web pages in Apache by implementing some rules to the .htaccess file.

Your Cart