Support >
  About independent server >
  301 redirect configuration method in Apache server
301 redirect configuration method in Apache server
Time : 2025-12-02 17:34:49
Edit : DNS.COM

In website operation, 301 redirects ensure search engine rankings and user experience after website structure changes. When a website permanently moves to a new address, the server returns a 301 status code to inform browsers and search engines of this change and transfers traffic and authority to the new location.

The 301 status code indicates that the requested resource has been permanently moved to the new URL. Unlike temporary redirects, it guides search engines to transfer the authority of the old page to the new page, which is crucial for SEO. In Apache servers, this is mainly achieved by modifying configuration files. Common methods include using the `mod_alias` module's `Redirect` directive and the more powerful `mod_rewrite` module.

The `mod_alias` module provides the simplest and most direct redirect method, suitable for basic needs. Its `Redirect` directive syntax is concise: `Redirect [status] old_path new_URL`. Setting `status` to `permanent` or `301` implements a 301 redirect.

For example, to redirect an old page to the corresponding page on the new domain:

Redirect 301 /old-page.html http://www.newdomain.com/new-page.html

When accessing `http://olddomain.com/old-page.html`, the server returns a 301 status code and points to the new address.

For migrating the entire site, you can configure:

Redirect 301 / http://www.newdomain.com/

This configuration redirects all requests from the original site to the root directory of the new domain, maintaining the original path structure.

To redirect a directory and its contents, use:

Redirect 301 /old-directory/ http://www.newdomain.com/new-directory/

Note the trailing forward slash in the source path; it ensures that only the contents of the directory are redirected, not the directory name itself.

These configurations can be placed in the Apache main configuration file (`httpd.conf`), virtual host configuration files, or `.htaccess` files. When using `.htaccess`, ensure that the `AllowOverride` configuration of the directory allows the use of the `Redirect` directive.

The `mod_rewrite` module provides finer-grained control through the `RewriteRule` directive, supporting regular expression matching and complex conditional judgments. Enabling it requires the module to be loaded and `RewriteEngine` to be running.

The basic syntax is: `RewriteRule pattern replacement [flag]`. The key flag for implementing a 301 redirect is `[R=301,L]`: R indicates redirection, 301 specifies the status code, and L indicates stopping processing of subsequent rules.

Example of redirecting an old page to a new page:

`RewriteEngine On RewriteRule ^old-page\.html$ /new-page.html [R=301,L]`

This rule uses a regular expression to precisely match `old-page.html`, redirecting to `new-page.html` on the same domain.

Regular expressions can be even more effective for batch changes to URL patterns. For example, redirecting a dynamic URL after converting it to static:

RewriteRule ^product-([0-9]+)\.html$ /products.php?id=$1 [R=301,L]

This rule redirects requests like `product-123.html` to `products.php?id=123`, preserving the product ID parameter.

`mod_rewrite` supports adding matching conditions via the `RewriteCond` directive for more precise control. For example, redirecting only when the old domain is requested:

RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

Here, `RewriteCond` checks if `HTTP_HOST` is `olddomain.com`, `[NC]` indicates case insensitivity, `(.*)` in `RewriteRule` captures the entire path, and `$1` is reused in the target URL.

Unifying the WWW and non-WWW versions of the entire website is a common requirement. Configuration for redirecting non-WWW to WWW:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Reverse redirection (WWW to non-WWW) only requires adjusting the target URL.

HTTP to HTTPS redirection is especially important when SSL is enabled:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

This rule checks if HTTPS is off; if so, it redirects the request to an HTTPS address on the same path.

Preserving the query string is often overlooked but important in redirects. By default, `RewriteRule` does not automatically pass the query parameters (the content after the question mark) from the original URL to the new address. To preserve them, there are two methods: one is to add the `[QSA]` flag (query string appending) to the rule, and the other is to manually pass them using the `%{QUERY_STRING}` variable.

When handling multilingual website redirects, you can automatically redirect based on your browser's language preference:

RewriteCond %{HTTP:Accept-Language} ^zh [NC]
RewriteRule ^/$ /zh/ [R=301,L]

This rule redirects the homepage to the Chinese version if the browser's language preference is Chinese.

After configuration, you must test the syntax before restarting the service. Execute `apachectl configtest` or `httpd -t` to check the syntax. After confirming that there are no errors, restart Apache using:

systemctl restart apache2

or

service httpd restart

to make the configuration take effect.

There are several ways to verify that the redirection is working correctly. Use the curl command to view detailed responses:

curl -I http://www.olddomain.com/old-page.html

The returned headers should contain an `HTTP/1.1 301 Moved Permanently` status line and `Location: http://www.newdomain.com/new-page.html` indicating the new address.

Online tools like Redirect Checker can also visually display the redirect chain. Browser developer tools' network panel can also observe the request-response process.

When implementing 301 redirects, avoiding redirect loops is crucial. Loops are often caused by improperly configured rules, such as A redirecting to B, and B redirecting back to A. Carefully check the logic during configuration. You can temporarily test with a 302 redirect to confirm it's correct before switching to 301.

Updating search engine indexes takes time. Even with correctly configured 301 redirects, the transfer of search engine ranking may take several weeks. During this time, monitor the search engine's website management tools to understand the progress.

Keeping the redirect chain simple is beneficial for performance. Ideally, any URL should reach its final address within a single redirect, avoiding multiple redirects. Regularly review redirect rules and remove unnecessary entries.

For a large number of URL redirects, consider the performance impact. Setting too many complex rules in .htaccess can increase server load. If handling thousands of redirects, it is recommended to use database mapping or a dedicated redirect module.

After configuration changes, thoroughly test all types of URLs, including addresses with query parameters, case variations, and links with special characters, to ensure that redirects work as expected under various conditions.

301 redirects are a fundamental technology for website maintenance. Correct implementation can ensure that the website does not lose traffic and search engine rankings during structural changes. Combined with the flexible configuration options of the Apache server, administrators can choose the most suitable implementation plan according to specific needs, ensuring a smooth transition for users and search engines to the new address.

DNS Amy
DNS Luna
DNS Jude
DNS Puff
DNS Becky
DNS Sugar
DNS Grace
DNS NOC
Title
Email Address
Type
Information
Code
Submit