Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchChoose one canonical hostname—usually https://example.com—and redirect every www request to it with a permanent 301. In Nginx, put a simple return in separate server blocks. In Apache, use a dedicated virtual host with Redirect permanent. Configure the HTTPS www endpoint with a certificate before redirecting, preserve $request_uri or the original request path, and verify that the canonical host serves the page without redirecting back.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
What the redirect should accomplish
For a site whose canonical address is the non-www hostname, all of these requests should resolve consistently:
As an Amazon Associate I earn from qualifying purchases.
http://www.example.com/http://www.example.com/docs/start?mode=fullhttps://www.example.com/https://www.example.com/docs/start?mode=full
Each should produce one permanent 301 response to the equivalent HTTPS URL on example.com. The final https://example.com request should return the application response directly. The path, trailing slash, and query string must remain intact.
Using one canonical hostname avoids maintaining several cache and search-engine versions of the same page. The redirect belongs in the virtual host or server block for the alternate hostname; the canonical host should be responsible for serving the application.
#1 Best Overall
Before changing the server
Choose the canonical name
Replace example.com in every example with your real domain. Decide whether the bare name or www is canonical before editing configuration. This guide assumes the bare name, such as example.com, is canonical.
Make both names resolve
DNS must direct both example.com and www.example.com to the server or proxy that will issue the redirect. A redirect cannot run if the alternate hostname never reaches your web server.
Plan certificate coverage
A browser completes TLS before it can receive an HTTP redirect. Therefore, the HTTPS endpoint for www.example.com needs a certificate whose subject or SAN covers www.example.com (or an appropriate wildcard). The certificate for the canonical HTTPS virtual host must cover example.com. A certificate for only the bare name cannot safely redirect an HTTPS www request.
Back up and validate
Save the current Nginx or Apache configuration and use the server’s normal syntax-check command before reloading. Apply the change with a graceful reload or the deployment procedure used by your system, rather than stopping the service unnecessarily.
Nginx: separate redirect and application server blocks
Nginx selects a server block from the listen address and the server_name. Define the two hostnames explicitly. Keep the redirect block for www separate from the block that serves the bare domain. Nginx’s simple return directive is preferable here to a conditional if or a rewrite rule.
HTTP requests
If HTTP is allowed to reach the server, redirect both HTTP names to the canonical HTTPS URL. The following blocks preserve the complete original request URI, including its path and query arguments:
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
The first block removes www and upgrades to HTTPS in one response. The second block upgrades an HTTP request that already uses the canonical hostname. If your site intentionally serves the bare hostname over HTTP, the second block can instead contain the site’s normal HTTP application configuration; do not change the www block into an application server.
HTTPS requests
The HTTPS www block must terminate TLS, then issue the redirect. The canonical HTTPS block terminates TLS and serves the application:
Rank #2
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example;
# Add the application's location or proxy rules here.
}
The certificate paths above are an example layout; use the paths created by your certificate-management process. The important requirements are that the certificate presented by the first block covers www.example.com and that the redirect is inside that TLS-enabled block.
Why $request_uri matters
Nginx’s $request_uri contains the original path and arguments. A request for /docs/start?mode=full therefore becomes https://example.com/docs/start?mode=full, not merely the home page. Do not replace it with a hard-coded slash when deep links must survive.
Recommended Free Tools
Preventing an Nginx loop
- Limit the redirect block’s
server_nametowww.example.com. - Do not place the
wwwredirect in the canonicalexample.comserver block. - Ensure the canonical HTTPS block serves content instead of redirecting to
www. - Check that a proxy or load balancer in front of Nginx is not rewriting the host back to
www.
A common loop occurs when both hostnames contain rules that point to the other, or when the canonical block also applies the alternate-host rule. Host-based separation avoids that ambiguity.
Handle unmatched host headers deliberately
An arbitrary Host header should not accidentally select the application block. Configure an intentional default server for unmatched names—often one that rejects the request or returns a minimal response—and test it separately. This keeps an unintended hostname from exposing the site or generating redirects for names you do not control.
Apache: use a virtual-host redirect with mod_alias
For a hostname-only redirect, Apache’s simplest and preferred approach is the Redirect directive in the virtual host for the non-canonical name. It avoids adding conditional rewrite logic where none is needed.
HTTP virtual hosts
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
With Redirect permanent / https://example.com/, Apache maps the requested path onto the target. Thus /docs/start is redirected to https://example.com/docs/start; the query string is retained. If your HTTP canonical virtual host serves content instead of forcing HTTPS, replace its redirect with the normal document-root or proxy configuration, while leaving the www virtual host as a redirect.
HTTPS virtual hosts
Add a certificate-bearing *:443 virtual host for www.example.com and put the same permanent redirect there. Configure a separate canonical *:443 virtual host to serve the application:
<VirtualHost *:443>
ServerName www.example.com
SSLEngine on
SSLCertificateFile /path/to/fullchain.pem
SSLCertificateKeyFile /path/to/privkey.pem
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /path/to/fullchain.pem
SSLCertificateKeyFile /path/to/privkey.pem
DocumentRoot /var/www/example
# Add the site's normal directory, proxy, and application settings here.
</VirtualHost>
Use the certificate and key locations supplied by your operating system or certificate manager. The www TLS virtual host must be selected during the handshake; otherwise the browser can fail with a certificate error before Apache has a chance to send the 301.
Rank #3
Apache alternative: mod_rewrite when conditions are required
Use mod_rewrite only when the redirect genuinely depends on conditions that a simple virtual-host redirect cannot express. Keep the condition limited to the alternate hostname and do not enable the same rule in the canonical host.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
RewriteCondprevents the rule from matching the canonical hostname.R=301makes the redirect permanent.Lstops further rewrite processing for this request.%{REQUEST_URI}carries the original path and query-related request target into the destination.NEprevents unwanted escaping of characters already present in the URL.
Place the rule in the www virtual host or the relevant document root, and verify that no other rewrite rule subsequently sends the request to a different hostname. For a straightforward hostname redirect, Redirect permanent remains easier to audit.
Free tools Windows power users keep installed
One-click scans. No signup required.
Combining www removal with HTTPS safely
There are four possible entry points: HTTP and HTTPS for each hostname. A clean configuration makes every non-canonical entry point converge on the final URL, normally in one redirect:
| Request | Expected result | Why |
|---|---|---|
http://www.example.com/path |
One 301 to https://example.com/path |
Removes www and upgrades to HTTPS together. |
http://example.com/path |
One 301 to https://example.com/path |
Upgrades the canonical hostname. |
https://www.example.com/path |
One 301 to https://example.com/path |
TLS is completed on the www endpoint, then the host is canonicalized. |
https://example.com/path |
Application response, no redirect | This is the final canonical URL. |
Do not create a first redirect from HTTP www to HTTPS www followed by a second redirect to the bare host unless your architecture makes that unavoidable. Combining both changes in the destination reduces redirect-chain length and makes testing easier.
How path and query preservation works
Hostname canonicalization should change only the authority and, when required, the scheme. The resource identifier after the host remains the same. That means /, /about/, and /docs/start must each map to the identical path on the canonical host, including whether a trailing slash was present. Query arguments such as ?mode=full are part of the request target and must also arrive at the destination.
In Nginx, $request_uri is the complete original URI used in the redirect target. In Apache, the path-prefix behavior of Redirect permanent / ... performs the equivalent mapping, while a rewrite rule should use %{REQUEST_URI}. A hard-coded destination ending only in / is appropriate only when every source URL should intentionally become the home page; it is wrong for a site migration where deep links must continue to work.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Check the first response rather than only the final browser address. A later application redirect can mask a missing path or query string, while the first Location header shows exactly what the web server generated.
Deployment details when a proxy is involved
If a reverse proxy, load balancer, or managed edge service sits in front of Nginx or Apache, both hostnames still need to be routed to the component that owns the redirect. The proxy may terminate TLS, or it may pass the TLS connection through; in either design, the HTTPS www endpoint must present coverage for www.example.com before a redirect can be returned.
Rank #4
Keep hostname decisions consistent across layers. A proxy that changes the incoming host to the bare name before forwarding can prevent the www virtual host from being selected. Conversely, an application that reconstructs links from an incorrect host or scheme can issue a second redirect or create a loop. Test the public URL, not just a direct request to the origin, and verify the host and scheme visible at each layer.
Why a separate block or virtual host is safer
A dedicated alternate-host endpoint makes the redirect’s scope obvious: it matches one hostname, returns one status, and does not run application routing. This is easier to review than a broad conditional that executes in every host context. It also leaves the canonical endpoint free to serve files or proxy requests normally.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsIn Nginx, this is why a separate server block is preferred over an if test shared with the application block. In Apache, the equivalent separation is a www virtual host containing Redirect permanent. Conditional rewrite rules remain useful when several conditions genuinely determine the destination, but they add another place where host matching and path handling can go wrong.
Verification procedure
- Validate syntax. Run Nginx’s normal configuration test, such as
nginx -t, or Apache’s normal configuration test, such asapachectl configtest. Correct any reported error before reloading. - Reload gracefully. Use the service manager or deployment process used by your server so existing connections are handled safely.
- Test the HTTP
wwwroot. Requesthttp://www.example.com/and confirm a single301with aLocationofhttps://example.com/. - Test a deep path. Request
http://www.example.com/docs/start. The destination must end in/docs/start, not just/. - Test a query string. Request a URL such as
http://www.example.com/docs/start?mode=fulland verify that the query appears on the destination. - Test HTTPS
www. Requesthttps://www.example.com/. Confirm the TLS handshake succeeds and the response is one301to the canonical HTTPS host. - Test the canonical host. Request
https://example.com/docs/start?mode=full. It should return the page or application response without redirecting towww. - Test an unknown host. Send a request with an unmatched
Hostheader and confirm it is handled by the intended default server rather than exposing the application.
Inspect both the status code and the exact Location header. A browser may cache a permanent redirect, so command-line or header-only testing is useful when checking a newly changed rule.
Common failures and fixes
The HTTPS www request shows a certificate warning
The redirect cannot be delivered until TLS succeeds. Install or select a certificate covering www.example.com, ensure the *:443 or listen 443 ssl virtual host is selected for that name, and reload the server.
The path disappears
A destination hard-coded as https://example.com/ without URI handling sends every request to the home page. In Nginx append $request_uri. In Apache use Redirect permanent / https://example.com/ or include %{REQUEST_URI} in a rewrite rule.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →The query string is missing
Check for a later rewrite, proxy, or application redirect that replaces the request target. Test the first response’s Location header directly and ensure no rule rebuilds the URL without its arguments.
There are two or more redirects
Inspect every HTTP and HTTPS host entry. The ideal destination is the final HTTPS bare-host URL. Remove a separate HTTPS-upgrade redirect for www when the initial www rule can point directly to the canonical HTTPS URL.
Best Value
The redirect loops
Confirm that the canonical server block or virtual host does not also match the www rule. If a reverse proxy is involved, verify which host and scheme it forwards to the web server; an application that believes the request is HTTP can repeatedly demand HTTPS.
The wrong site answers
This usually means a missing or mistyped server_name, ServerName, or virtual-host ordering issue. Define both names explicitly, check which default server handles unmatched names, and reload after correcting the configuration.
Nginx and Apache implementation comparison
| Concern | Nginx | Apache |
|---|---|---|
| Where the rule belongs | A dedicated server block for www.example.com |
A dedicated virtual host for www.example.com |
| Simple directive | return 301 https://example.com$request_uri; |
Redirect permanent / https://example.com/ |
| Conditional logic | Possible with rewrite logic, but unnecessary for this hostname-only case | mod_rewrite with a restricted HTTP_HOST condition |
| Path and query preservation | $request_uri |
Redirect path mapping or %{REQUEST_URI} |
| HTTPS requirement | The www listen 443 ssl block needs a certificate covering www |
The *:443 www virtual host needs a certificate covering www |
| Canonical host | Separate HTTPS block serves the application | Separate HTTPS virtual host serves the application |
| Operational check | Run the normal Nginx syntax test, then reload | Run the normal Apache syntax test, then graceful reload |
Operational checklist
- Both DNS names reach the intended server or proxy.
- The canonical hostname is documented as
https://example.com. - HTTP and HTTPS entry points are configured deliberately.
- The HTTPS
wwwendpoint has valid certificate coverage. - The alternate-host redirect uses status
301. - Paths, trailing slashes, and query strings survive the redirect.
- The canonical HTTPS host serves content without redirecting back.
- Unknown host headers do not expose the application.
- Configuration syntax was checked before reload.
Frequently Asked Questions
How do I redirect www to non-www in Nginx?
Create a separate Nginx server block with server_name www.example.com and return 301 https://example.com$request_uri;. Give the canonical hostname its own HTTPS block that serves the application.
How do I redirect www to non-www in Apache?
Put Redirect permanent / https://example.com/ in the www.example.com virtual host. Add a certificate-bearing *:443 virtual host for HTTPS and keep the canonical virtual host separate.
Will the redirect preserve a deep URL and its query string?
Yes. Nginx preserves them with $request_uri; Apache’s path-prefix form preserves the requested path and query string. Verify the first Location header for a URL such as /docs/start?mode=full.
Why does HTTPS www need a certificate if it only redirects?
TLS negotiation happens before the server can send the HTTP 301. The certificate presented for www.example.com must therefore cover that hostname, even though the endpoint serves no application content.
How can I avoid a redirect loop?
Match the redirect only on www.example.com, never apply it in the canonical host, and ensure any proxy forwards the original host and scheme consistently. The canonical HTTPS endpoint must serve the response directly.
Should I use Nginx rewrite or Apache mod_rewrite for this?
For a simple hostname change, use Nginx return or Apache Redirect permanent. Use conditional rewrite logic only when the destination genuinely depends on additional conditions.
The Bottom Line
Define one canonical HTTPS hostname and make every alternate entry point reach it directly. In Nginx, use separate server blocks with return 301; in Apache, use separate virtual hosts with Redirect permanent. Cover the HTTPS www name with a valid certificate, preserve the request URI, validate before reload, and confirm the canonical host serves content without creating another redirect.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

