I'm switching servers and need to change the following RewriteCond & RewriteRule commands in the .htaccess file.
Right now I'm pointing domain.com to www.domain.com

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

I need to switch the commands and do the opposite where I point www.domain.com to domain.com.
I would appreciate some help with the syntax. Thank you

Dani AI

Generated

A few practical clarifications and a safe workflow based on the thread: is directionally correct — a host-based check and a redirect are the right idea — and the escaping/quoting of http:// is unnecessary. 's caution about losing VPS access is valid; a permanent 301 is cached by browsers and CDNs and should only be applied after verification. Important points: enable RewriteEngine if it is not already on, place the redirect before application-specific rules, and keep a backup of the current .htaccess before editing.

A compact, safer test setup that preserves the requested host (removes the leading "www.") while preserving scheme (http vs https) is shown below. Start with a temporary redirect (302) to verify behavior; switch to 301 only after confirming results.

RewriteEngine On

# test mode: redirect hosts beginning with "www." to the bare host, preserve path/query and scheme
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

Safety and troubleshooting checklist: create a copy (for example .htaccess.bak) before changes; keep an active SSH/SFTP session or console available so the backup can be quickly restored if needed; verify redirects with a curl command (curl -I) to inspect Location headers and response codes (avoids browser cache issues); check Apache error/access logs if rules do not fire. For best performance and reliability, prefer a virtual-host (ServerName/ServerAlias) configuration in the main Apache config instead of per-directory .htaccess when access to the server config is available. More details and flags are in the Apache mod_rewrite documentation (https://httpd.apache.org/docs/current/mod/mod_rewrite.html).

Recommended Answers

All 3 Replies

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

Have you tried simply swapping out where the www is?

OK, I'll try this. Are you sure this is the right syntax.
I'd rather not experiment, since I'm following specific instructions from my host provider and at some point loose access to the VPS that I'm adding this code to.

I might be wrong about escaping the http:// part. I removed the double quotes that you had b/c I had never seen that before. This should work:

RewriteCond     %{HTTP_HOST}    ^domain.com$                [NC] 
RewriteRule     ^(.*)$      http://www.domain.com/$1        [R=301,L] 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.