How to Redirect a Web Page with Apache
To redirect web pages in Apache you should add specific rules to a `.htaccess` on an Apache web server. Learn how to do it.
The most common way of redirecting a web page is to add specific rules to a .htaccess file on the Apache web server. The .htaccess file allows you to make configuration changes on a per-directory basis. You need to create a .htaccess file or modify an already existing one and place it in the 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 .htaccess file:
Example
How to Redirect a Web Page with Apache
Redirect / http://www.domain2.comThis instructs the browser to direct all requests for the current directory to "www.domain2.com". As we have already mentioned, it is only for a single page or directory, not for complex website-wide routing.
Since we’ve used no status argument, the redirect defaults to 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:
How to Redirect a Web Page with Apache
Redirect 301 /oldlocation http://www.domain2.com/newlocationHow to Redirect a Web Page with Apache
Redirect permanent /oldlocation http://www.domain2.com/newlocationYou can use the status argument to specify other HTTP status codes. The syntax is Redirect [status] URL-path URL, where both the path and destination URL are always required.
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
How to Redirect a Web Page with Apache
RedirectMatch 301 /blog(.*) http://www.blog.new-website.com$1This 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.
Unlike mod_alias, mod_rewrite is primarily designed for internal URL rewriting rather than external redirects. The main difference is that internal rewriting changes the URL on the server side without notifying the client, while external redirects instruct the client to request a new URL.
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, ensure the module is loaded in your Apache configuration (e.g., a2enmod rewrite on Debian/Ubuntu or uncommenting LoadModule rewrite_module in httpd.conf). Then, always start your rules in your .htaccess file with:
How to Redirect a Web Page with Apache
RewriteEngine onThe 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:
How to Redirect a Web Page with Apache
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:
How to Redirect a Web Page with Apache
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.
When used without the [R] (redirect) flag, mod_rewrite performs an internal rewrite. This means the browser’s address bar remains unchanged, and users won’t notice that the underlying page has been renamed or moved.
Example
How to Redirect a Web Page with Apache
RewriteEngine On
RewriteRule ^oldpage\.html$ /newpage.html [R=301,L]This example permanently redirects a request for oldpage.html to newpage.html. The [R=301,L] flags specify a permanent external redirect and stop processing further rules.