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.
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.
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:
<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.
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:
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.
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.
To enable mod_rewrite, you must always use the following as the first line of the rules in your .htaccess file:
The mod_rewrite module uses the RewriteRule and RewriteCond directives.
This directive specifies rules for the rewriting engine and follows this syntax:
Sometimes RewriteRule is prefaced by RewriteCond to specify a condition under which the rewriting will take. The RewriteCond directive follows this syntax: