Generic .htaccess Redirect: Implementation and Analysis of WWW to Non-WWW Domain

Nov 08, 2025 · Programming · 15 views · 7.8

Keywords: .htaccess | redirect | mod_rewrite | domain_normalization | Apache_configuration

Abstract: This article provides an in-depth exploration of implementing generic redirects from WWW to non-WWW domains using .htaccess files in Apache servers. Through analysis of the core mechanisms of the mod_rewrite module, it explains the principles of RewriteCond condition matching and RewriteRule rewriting in detail, offering complete code implementation and configuration instructions. The article also discusses performance optimization, security considerations, and common troubleshooting methods during the redirection process, providing web developers with a comprehensive and reliable domain normalization solution.

Technical Background and Problem Definition

In modern web development, domain normalization is a crucial aspect of search engine optimization and user experience. When a website is accessible both with and without the WWW prefix, it can lead to content duplication and weight dispersion issues. Apache server's .htaccess file, combined with the mod_rewrite module, provides a flexible URL rewriting solution.

Core Implementation Principles

The essence of the generic redirection solution lies in dynamically capturing domain information to avoid hardcoding. By using regular expressions to match the HTTP_HOST server variable and extract the main domain part:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Here, %{HTTP_HOST} retrieves the current request's domain, the regular expression ^www\.(.*)$ matches domains starting with WWW, and %1 back-references the captured domain body.

Detailed Code Analysis

Let's analyze this redirection configuration line by line:

RewriteEngine On: Enables the URL rewriting engine, which is a prerequisite for all rewriting rules.

RewriteBase /: Sets the base path for rewriting to the root directory, ensuring correct resolution of relative paths.

RewriteCond Directive: Conditional statement using the %{HTTP_HOST} server variable and regular expression for pattern matching:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

Regular expression breakdown: ^ indicates string start, www\. matches "www." literally (dot requires escaping), (.*) captures any character sequence, $ indicates string end. [NC] flag makes matching case-insensitive.

RewriteRule Directive: The actual rewriting rule:

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Here, ^(.*)$ matches the entire request path, %1 references the domain body captured in the condition, $1 references the path matched in the rule. [R=301,L] flags indicate 301 permanent redirection and termination of subsequent rule processing.

Protocol Selection and Security Considerations

The original solution uses HTTP protocol, but modern web development recommends HTTPS:

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

This configuration ensures redirection to secure HTTPS connections while maintaining domain normalization. If the source request is already HTTPS, this configuration remains effective since the redirection target explicitly specifies the protocol.

Performance Optimization Recommendations

Using rewrite rules in .htaccess affects server performance because each request requires reading and parsing the file. For high-traffic websites, it's recommended to move rules to virtual host configuration:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect 301 / http://example.com/
</VirtualHost>

This configuration loads when the server starts, offering better performance but lacking the flexibility of .htaccess.

Error Troubleshooting and Testing

Common issues include:

Testing method: Use curl command to verify redirection:

curl -I http://www.example.com/path

Should return 301 Moved Permanently status code and correct Location header.

Extended Application Scenarios

The same technical principles can be applied to other domain normalization scenarios:

By adjusting regular expressions and redirection targets, various domain management needs can be flexibly addressed.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.