Search code examples
.htaccessurl-rewritingsubdomain

.htaccess redirect www to non-www for subdomain that is before the www


I need to make a request to https://subdomain.www.domain.com rewrite the URL to https://subdomain.domain.com

Which part of the below rewrite do I change to achieve this? I'm guessing I have to put the subdomain in front of the www, but what do I put there, also how do I capture all subdomains - what's the general rule for all subdomains?

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

Solution

  • You may use this rule:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(domain\.com)$ [NC]
    RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]