How to Redirect a Web Page with Apache

The most common way of redirecting a web page is to add specific rules to .htaccess file on the Apache web server. The .htaccess file is a way of allowing to make configuration changes on a per-directory basis. You need to create a .htaccess file or modify an already existing one and add it to the old website’s root directory.

Apache (Apache HTTP Server) can redirect a web page using different tools. Both mod_alias and mod_rewrite modules can be used in a .htaccess file to redirect a website.

Redirecting with mod_alias

The mod_alias handles simple URL manipulation tasks. It provides the Redirect and RedirectMatch directives as a means to redirect one URL to another. This kind of redirection must be done with these directives instead of RewriteRule.

Use the Redirect Directive

The Redirect directive lets you execute simple and one-page redirects with Apache. It connects an old URL with a new one by asking the client to fetch the resource again at the new location. The Redirect directive requires at minimum two arguments: the old URL and the new URL.

To accomplish the redirect, add the following lines to your server configuration:

Example

<VirtualHost *:80>
	ServerName www.domain1.com
	Redirect / http://www.domain2.com
</VirtualHost>

<VirtualHost *:80>
	ServerName www.domain2.com
	. . .
	. . .
</VirtualHost>

This instructs the browser to direct all requests for "www.domain1.com" to "www.domain2.com". As we have already mentioned, it is only for a single page, not for the entire website.

Since we’ve used no status argument, the redirect is temporary, i.e. HTTP status 302. This means a temporarily moved resource. In the case you want to have a permanent redirect, use either of the following lines:

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

So, you can use the status argument to specify other HTTP status codes. The URL argument should be present If the status is between 300 and 399. If the status is not between the mentioned range, the URL argument should be omitted.

Use the RedirectMatch Directive

If you want to redirect more than one page, use the RedirectMatch directive. It allows including a regular expression in your redirection criteria and provides some benefits of using RewriteRule.

The RedirectMatch directive matches patterns in parentheses and sets the matched text in the redirect using "$1" where 1 is the first group of text. Subsequent groups are given numbers sequentially.

If you want to match each request for something within the "/blog" directory to a subdomain named "blog.new-website.com", you can use the following:

Example

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

This command will redirect the blog of the old website to the blog of a new one.

Like the Redirect directive, here also you can specify the redirect type by adding the redirect status code before the URL location rules.

Redirecting with mod_rewrite

To accomplish more complicated tasks such as manipulating the query string, you need the tools provided by the mod_rewrite module. It uses a rule-based rewriting engine to rewrite the requested URLs. By default, it maps the URL to a filesystem path. Also, it can be used to redirect a URL to another URL.

The mod_rewrite module allows using limitless rules, and each rule can have several attached rule conditions to allow rewriting the URL based on environment variables, server variables, HTTP headers and time stamps.

It doesn't redirect but rewrites URLs. The main difference between rewriting and redirecting is that rewriting a URL involves the server returning a different request than the one provided by the client.

The mod_rewrite module must be considered a last option when other alternatives can’t be used. Using it when there are other simpler alternatives can lead to configurations that are fragile and difficult to maintain.

To enable mod_rewrite, you must always use the following as the first line of the rules in your .htaccess file:

RewriteEngine on

The mod_rewrite module uses the RewriteRule and RewriteCond directives.

Use the RewriteRule Directive

This directive specifies rules for the rewriting engine and follows this syntax:

RewriteRule [pattern] [substitution] [flags]
  • Pattern interprets the requested URL by using regular expressions.
  • Substitution is the actual URL of the page, which consists of the information you want to display. It can be a full filesystem path, a web path that is relative to the document root, or an absolute URL.
  • Flags are optional, and they present tags at the end of the RewriteRule directive, which can change the expression’s behavior.

Use the RewriteCond Directive

Sometimes RewriteRule is prefaced by RewriteCond to specify a condition under which the rewriting will take. The RewriteCond directive follows this syntax:

RewriteCond [test string] [condition] [flags]
  • Test string is normally a server variable having the format %{VARIABLE NAME}.
  • Condition can be a regular expression, a string comparison, or a file/path test.
  • Flags are here optional too.

The mod_rewrite module doesn’t change the content of the browser’s address bar, so users of the old URL even won’t recognize that the page has been renamed.