Øle Bjarnstroem Øle Bjarnstroem - 4 months ago 6
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]
</IfModule>


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

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]
Comments