From: Rich Bowen
Date: Mon, 11 May 2026 17:18:52 +0000 (+0000)
Subject: Rewrite guide: merge www-resolve into canonicalhost section
X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e26812cc1653731ceac0fb5d79a7b7d25d178088;p=thirdparty%2Fapache%2Fhttpd.git
Rewrite guide: merge www-resolve into canonicalhost section
The two sections covered the same hostname canonicalization concept
from slightly different angles. Merge the "Remove www" recipe and
SEO rationale into the existing canonicalhost section and remove
the standalone www-resolve section.
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1934112 13f79535-47bb-0310-9956-ffa450edef68
---
diff --git a/docs/manual/rewrite/TODO.md b/docs/manual/rewrite/TODO.md
index 8cc356cffd..0439af1a8f 100644
--- a/docs/manual/rewrite/TODO.md
+++ b/docs/manual/rewrite/TODO.md
@@ -28,7 +28,7 @@ result in one file "owning" the content and others cross-referencing it.
warning boxes within the [P] flag section.
â Remove the literal duplicate.
-- [ ] **Two hostname canonicalization sections in remapping.xml** â
+- [x] **Two hostname canonicalization sections in remapping.xml** â
"canonicalhost" and "www-resolve" cover the same concept.
â Merge "www-resolve" into "canonicalhost".
diff --git a/docs/manual/rewrite/remapping.xml b/docs/manual/rewrite/remapping.xml
index daced54a5a..42050a88c1 100644
--- a/docs/manual/rewrite/remapping.xml
+++ b/docs/manual/rewrite/remapping.xml
@@ -184,65 +184,6 @@ RewriteRule "^" "%1" [R=301,L]
-
-
- Canonical www/non-www Hostname
-
-
- - Description:
-
- -
-
You want to force all requests to use either
- www.example.com or example.com,
- not both. This ensures search engines treat them as one site
- and prevents cookie scope issues.
-
-
- - Solution:
-
- -
-
The best approach does not use mod_rewrite at
- all. Place a Redirect
- in the virtual host for the non-canonical hostname:
-
-
-# Redirect example.com -> www.example.com
-<VirtualHost *:80 *:443>
- ServerName example.com
- Redirect permanent "/" "https://www.example.com/"
-</VirtualHost>
-
-
- If you only have .htaccess access:
-
-
-# Add www
-RewriteEngine On
-RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
-RewriteRule "^(.*)" "https://www.%{HTTP_HOST}$1" [R=301,L]
-
-
-
-# Remove www
-RewriteEngine On
-RewriteCond "%{HTTP_HOST}" "^www\.(.+)$" [NC]
-RewriteRule "^(.*)" "https://%1$1" [R=301,L]
-
-
-
-
- - Discussion:
-
- -
-
See also the Canonical Hostnames
- recipe above, which covers the general case. This recipe focuses
- specifically on the www/non-www choice, which is the most common
- hostname canonicalization need.
-
-
-
-
-
Front Controller / Application Routing
@@ -497,6 +438,16 @@ RewriteRule "^/?(.*)" "http://www.example.com/$1" [L,R,NE]
example.com, you could use the following
recipe:
+To do the reverse - strip the www. prefix - swap the
+condition:
+
+
+RewriteCond "%{HTTP_HOST}" "^www\.(.+)$" [NC]
+RewriteRule "^(.*)" "http://%1/$1" [L,R,NE]
+
+
+To generically add www. to any hostname:
+
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
@@ -514,7 +465,11 @@ RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
If you have access to the server configuration, a
Redirect in a dedicated
VirtualHost
- is the cleanest approach. Use the
+ is the cleanest approach. Canonicalizing the hostname ensures that
+ search engines treat your site as a single entity and avoids
+ cookie scope issues that arise when the same site is reachable
+ under multiple names.
+ Use the
If directive
as a middle ground, and mod_rewrite only if you
are limited to .htaccess.