regex.htaccesshttp-redirectmod-rewrite

.htaccess wildcard subdomain redirect with wildcard path 404 not found


I'm trying to redirect wildcard subdomains with a wildcard path and it seems to be working, however when the url has any path a 404 error is returned.

For example https://test.mydomain.com/ will redirect with no issues to https://new.mydomain.com/ while https://test.mydomain.com/test will return a 404 error not found. I need https://test.mydomain.com/test to redirect to https://new.mydomain.com/test

My current .htaccess file is as follows

RewriteEngine on

RewriteCond %{HTTP_HOST} ^\*\.mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.\*\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ "https\:\/\/new\.mydomain\.com\/$1" [R=301,L]

Any help would be appreciated. Thanks


Solution

  • The issue might be that the redirect is being redirected, i.e. when test is redirected to new, that is being redirected again to new because of the * in the condition. Try editing as follows to not apply it to new.

    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
    RewriteCond %1 !^new$