Freelance consultants often manage multiple client sites where URL consistency is critical for branding and SEO. This article outlines the practical steps for implementing HTTP 301 redirects to unify www and apex domains, using Apache server configurations as a reference.
Understanding the Need for URL Unification
As a hypothetical illustration, picture a freelance consultant whose client roster includes a landscape photographer and a small accounting practice. Each owns two domains: a www host and an apex host. The photographer's marketing material says www. and the booking link says apex; the accountant's email signature says apex while the invoice portal says www. Both consultants receive similar complaints: "the link opened a different version of your site." The consultant decides to treat one form as canonical and redirect the other.
The first working artifact is a short inventory sheet with columns: project, host form that should win, host form that should redirect, redirect status, notes. Two example rows the consultant writes for the photographer and the accountant might read:
• Photographer — canonical: www host; redirecting: apex host; status: 301; notes: business cards and portfolio links use www. • Accountant — canonical: apex host; redirecting: www host; status: 301; notes: printed letterhead uses apex.
Second, a one-page decision memo naming each candidate and why it loses or wins. The memo might list three candidate labels: "www wins," "apex wins," and "leave both live." For the photographer the memo picks "www wins" because the domain appears that way on printed cards and in a directory listing the photographer already submitted; for the accountant it picks "apex wins" because the practice's letterhead and email domain read that way. "Leave both live" is rejected in both cases for the same stated reason: two live addresses mean two places to update and two sets of links that can drift apart.
Third, a redirect plan table with source path, destination path, status code, and owner. The consultant adds two fictional rows: source "/portfolio" on the redirecting host to destination "/work" with status 301, owner photographer; source "/contact" on the redirecting host to "/about" with status 301, owner accountant. These are the consultant's own worked examples for explaining the request, not records of completed changes.
The point of the memo is to give each client one sentence they can repeat to anyone who asks which address is correct, and to give the consultant a single table to check before publishing any new page.
Using Redirect Directives in Virtual Hosts
In Apache httpd 2.0, the mod_alias Redirect directive can be placed in a virtual host context, where it asks the client to refetch a resource at a new URL. The syntax is Redirect, an optional status, the URL-path, and the URL. The old URL-path is a case-sensitive decoded path beginning with a slash, and relative paths are not allowed. The new URL should be absolute, beginning with a scheme and hostname. Without a status argument the redirect is temporary, returning HTTP status 302; the permanent argument returns 301, indicating the resource has moved permanently. RedirectPermanent is exactly equivalent to Redirect permanent. When multiple Redirects occur in the same virtual host section, they are processed in the order they appear, with the first match taking precedence, so the most specific path must be listed first.1
Before touching any virtual host configuration, you can turn the two client examples into the next concrete action: write one redirect sentence per project that names the host form you will keep and the one you will point to it. For the photographer, that sentence might read, “Keep the www host as the address clients publish; send the apex host to it with a 301.” For the accountant, it becomes, “Keep the apex host; send the www host to it with a 301.” Writing each as a full statement, rather than a fragment like “apex → www,” gives you something you can paste into the plan table’s notes column and reuse when you draft the vhost block.
You might then convert each sentence into a short configuration sketch you can review before editing live files. For the photographer’s chosen case, jot down the desired behavior in plain words first: any request arriving on the apex host should be answered with a permanent redirect to the same path on the www host, and then confirm the sketch keeps the path unchanged so portfolio links do not break. If a sketch silently changes a path or drops a query string, revise the sentence before revising the configuration.
Consider keeping a small “decided this round” note listing what you changed, what you deliberately left alone, and which client sentence matches each entry. This gives you a trail to revisit at the next renewal or site launch, and it keeps the redirect logic tied to a stated decision rather than to a one-off edit.
Configuring mod_rewrite for Canonical Hostnames
Apache’s mod_rewrite documentation presents canonical hostnames as a case where you can force use of a preferred hostname over other hostnames that reach the same site. It states that the very best solution does not involve mod_rewrite: place a Redirect directive in a virtual host for the non-canonical hostnames.2
When mod_rewrite is used, the documentation gives separate recipes for sites on port 80 and on other ports. For port 80, the conditions test that the host is not the preferred www host and is not empty, then a rule with the L, R, and NE flags redirects the request. The documentation says these rulesets work either in the main server configuration or in a .htaccess file in the DocumentRoot.2
Before editing any server file for the photographer, you can draft a short review note that pairs each rule with the sentence it serves. Start with the host rule you already decided: the www host is canonical, the apex host redirects with a 301. Beneath it, write one line noting that the rewrite condition should match only the apex hostname, not every incoming request, so the rule does not loop back on itself. Add a second line recording which directory or site the rule applies to, so you can find it later among the client's other entries.
You might then sketch a before-and-after wording you can check against the existing configuration. For example, a vague note like "redirect apex to www" could become "requests arriving at the apex hostname return a 301 to the same path on the www hostname." Rewriting it this way gives you something you can compare line by line with the file, rather than a reminder you have to reinterpret later.
As a third optional step, prepare a small rollback entry alongside the change: the file and line you plan to touch, the original line copied verbatim, and the date you intend to make the edit. This is a plan, not a record of a completed edit, so you can leave the date blank until you act. If you later decide the accountant's apex-canonical case needs its own mirror rule, that becomes a separate entry rather than an edit to the photographer's note.
Keep the note short enough to reread in one sitting. When you do open the configuration, you can work from the sentence first and the sketch second, checking that each rewrite condition matches the host form you named and nothing broader.
Handling Port-Specific Redirection Rules
For a site running on a port other than 80, the Apache mod_rewrite documentation gives a rule set that adds the requested port back into the redirect target. The conditions check that the requested host does not begin with www, that the host is not empty, and that the server port is not 80; the rule then redirects to the www hostname using the server port variable. For a site running on port 80, the documented rule set omits the port and redirects to the www hostname with the captured request path.3
The documentation states these rule sets can be used in the main server configuration file or in an .htaccess file placed in the server's DocumentRoot. It also notes that many of its examples will not work directly in a particular server configuration, so the conditions matter rather than copied rules.3
For each project, you can turn the redirect sentence into a port-specific rule and note which port it serves, so the www-to-apex decision stays consistent whether a visitor arrives on the standard port or another one. Draft the rule as a pair: the condition you expect and the destination you want. A vague line such as “redirect the other port to www” could become “requests arriving on port 8080 for the apex host should go to the same path on the www host with a 301.” Then read the sentence aloud against the inventory row you already completed; if the destination host differs from the row’s winning form, the sketch is the thing to revise.
You might keep a separate review column for port rules rather than folding them into the main host table, so a later reader can see at a glance which entries depend on a port number and which do not. For the accounting example, the port note could read “port 8443, apex path preserved, 301, owner: consultant,” matching the memo sentence that names apex as canonical. Leave the photographer’s entry blank if no port rule applies there, and write “not applicable” instead of guessing, since an empty cell and a deliberate no-rule note mean different things when you return to the file.
Consider adding one line beneath each port sketch describing what you deliberately did not change, such as an unrelated staging port, so the note records a choice rather than an omission. Keep the whole page to a few short rows; you can reread it before opening any configuration file and adjust the wording first.
Implementing Conditional Redirects with If Directives
Apache HTTP Server documentation shows an If directive alternative for enforcing a canonical hostname, available in version 2.4 and later. The condition compares the HTTP_HOST server variable against the desired host, and the enclosed Redirect directive sends requests to that host. The documented example redirects when the host is not www.example.com, with the Redirect directive pointing from the root path to the canonical host URL.3
The same If directive is also documented for redirecting part of a site to HTTPS, using a condition on the SERVER_PROTOCOL variable rather than the host, with the example redirecting the admin path. The page presents these as examples that may not work unchanged in a particular server configuration, so the condition and path values should be checked rather than copied directly.3
For each project, you can turn the redirect sentence into a port-specific rule and note which port it serves, so the www-to-apex decision stays consistent whether the connection arrives on the standard port or a secure one. A short field set works well here: project, port, host that should win, host that should redirect, status, and the sentence the rule serves. One filled example might read: photographer, port 443, www host wins, apex redirects, 301, “The portfolio address is the www form.” The same fields fit a second condition example, such as a plain-port entry, without changing the underlying choice.
You might keep a separate review column for port rules rather than folding them into the main host table, so a later reader can see at a glance which entries depend on the port and which are plain host decisions. If the photographer’s apex rule is meant only for the secure port and the plain port stays as it is, record that as a deliberate exclusion rather than an omission.
Consider adding one line beneath each port sketch describing what you deliberately did not change, such as an unrelated staging port, so the note records a choice instead of an oversight. Then, before editing, reread the sentence and the sketch together; if the port line and the sentence disagree, adjust the sentence first and let the rule follow.
Best Practices for Maintaining Redirect Consistency
For maintaining consistency after the first round of changes, you can start with the “decided this round” note and expand it into a one-paragraph status line per project. A photographer entry might read: “www host is canonical; apex host returns 301; no path-level redirects were added this round; the client sentence is unchanged.” An accountant entry might read: “apex host is canonical; www host returns 301; the old contact path still needs review; the client sentence remains the one already agreed.” Writing these in plain prose makes it easier to spot an entry that quietly contradicts the decision memo.
You might then build a small consistency review sheet with columns you choose, such as project, sentence version, rule type, intended result, and note. Fill it with fictional values: photographer, “use www,” host rule, apex sends 301 to www, reviewed. Then add an accountant row: accountant, “use apex,” host rule, www sends 301 to apex, pending path check. The sheet is a reading aid, not a record of a completed test, and you can keep or discard any column that does not help you compare entries.
Consider a short drift check you can run by reading alone. Put the sentence, the review sheet row, and your configuration sketch side by side, then ask whether all three name the same winning host and the same 301 direction. If the sentence says www but the sketch still points the other way, mark the discrepancy rather than rewriting the sentence to match the sketch. You can decide which one should change before touching any file.
You might also keep a “do not disturb” line for each project listing items intentionally left alone, such as an unrelated staging host or a path serving a different site. When a later reader asks why a rule seems missing, that line explains the gap without requiring a new investigation. Keep the line short and concrete, and consider dating it so you can tell an old omission from a new one.
Finally, you can choose a simple closing habit: once per project, reread the sentence and the status line together and note one word or direction that changed. That note becomes the starting point for the next review, and you can stop there if nothing else needs attention.
Frequently asked questions
What columns should I put on my inventory sheet?
The first working artifact is a short inventory sheet with columns: project, host form that should win, host form that should redirect, redirect status, notes.
What might I list in a one-page decision memo that names each candidate and why it wins or loses?
Second, a one-page decision memo naming each candidate and why it loses or wins. The memo might list three candidate labels: "www wins," "apex wins," and "leave both live."
What fields can I use to turn the redirect sentence into a port-specific rule for each project?
For each project, you can turn the redirect sentence into a port-specific rule and note which port it serves, so the www-to-apex decision stays consistent whether the connection arrives on the standard port or a secure one. A short field set works well here: project, port, host that should win, host that should redirect, status, and the sentence the rule serves.
mod_alias - Apache HTTP Server . Retrieved 2026-11-21. ↩︎
Redirecting and Remapping with mod_rewrite - Apache HTTP Server Version 2.5 . Retrieved 2026-11-21. ↩︎ ↩︎
使用 mod_rewrite 进行重定向和重映射 - Apache HTTP 服务器 版本 2.5 . Retrieved 2026-11-21. ↩︎ ↩︎ ↩︎ ↩︎