Øle Bjarnstroem Øle Bjarnstroem - 1 year ago 78
PHP Question

How to redirect www to non-www while keeping URL with .htaccess

I try to redirect all requests on any URI with a www to the same URL without www. So

The rules I'm using at the moment are

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

But it only seems to work on the main domain, not on subpages.

Also important: Other subdomains like static.domain.com should still work.

Thanks for your help!

Answer Source

Try this rule with %{REQUEST_URI} instead:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download