Score:1

After installing script all subdomains go to the main domain

us flag

I have a main domain www.example.com.

and the following subdomains under the main domain

support.example.com
test.example.com

Previously both subdomains are working fine.

But after installing a PHP script on the main domain, only the main domain is working and all subdomains are going to main domain.

After checking .htaccess file I found this code:

RewriteEngine  on  
RewriteBase /  
RewriteCond %{THE_REQUEST} /(.+?)\.php[\s?] [NC]  
RewriteRule ^ %1 [R=301,L,NE]  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{DOCUMENT_ROOT}/$1.php -f  
RewriteRule ^(.+?)/?$ $1.php [L]  
RewriteCond %{HTTPS} off [OR]  
RewriteCond %{HTTP_HOST} !^www\. [NC]  
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]  

IndexIgnore *   

Is this related to the not working subdomains?

kz flag
How exactly are the subdomains "not working"? "all subdomains are going to main domain" - do you mean they are showing the same content as the main domain or are being externally redirected to the main domain? Where do the subdomains point to on the filesystem in relation to the main domain. Installing a "script" can't change the DNS, so I assume the subdomains are either pointing to the same place or perhaps a subdirectory?
srisree avatar
us flag
Hello MrWhite all subdomains are externally redirected to maindomain subdomain pointed to a directory in public_htmnl for example subdomain is support https://support.maindomain.com is url i have installed joomla also but after installation of one script in maindomain.com it was changed tried to open https://support.maindomain.com from browser it is going to https://maindomain.com all the files in subdomain directory are there created another subdomain testing. https://testing.maindomain.com also going to https://maindomain.com support subdomain pointed to public_html/support
kz flag
Have you tried the suggestion I posted in my answer? However, if you have Joomla installed in a subdirectory (that the subdomain points to) then this should have it's own `.htaccess` file with mod_rewrite directives (required for the Joomla front-controller) and this will override the directives in the parent `.htaccess` file (that belongs to the main domain) - so the directives posted above should not affect the `support` subdomain that points to the Joomla website.
kz flag
However, unless the `testing` subdirectory (to which the `testing` subdomain points to) also has it's own `.htaccess` file then the above directives will affect the `testing` subdomain (as mentioned in my answer).
kz flag
However, in your comment above you state that the request is going to `https://maindomain.com`, but the directives posted above redirect requests to `https://www.maindomain.com` (ie. the `www` subdomain) - please clarify. Otherwise, simply installing a "PHP script" in the main domain cannot interfere with the subdomains (except for `.htaccess` directives in a parent directory), unless the installation process changed the `support` and `testing` subdirectories in some way?
kz flag
(Tip: Use backticks to surround inline-code and URLs in comments. `like this`)
Score:0
kz flag
RewriteCond %{HTTPS} off [OR]  
RewriteCond %{HTTP_HOST} !^www\. [NC]  
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

You've not stated exactly what is happening or where the subdomains are pointing to, so there could be other issues here.

However, if your subdomains point to the same area of the filesystem (or perhaps a subdirectory) as the main domain then the above rule will redirect all subdomains to www.example.com - so the subdomain is "lost".

Depending on your requirements, you could resolve this by only checking for example.com instead of !^www\. and splitting the HTTP to HTTPS redirect into its own rule. For example:

# HTTP to HTTPS (including subdomains)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Canonical non-www to www redirect for main domain only
RewriteCond %{HTTP_HOST} ^(example\.com)\.?$ [NC]  
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

You will need to clear your browser cache before testing. (Preferably test with 302 - temporary - redirects to avoid potential caching issues.)

However, the directives are also in the wrong order (this has nothing to do with the subdomain issue). The canonical redirects (above) need to go before the internal rewrites. So, your .htaccess file should be ordered like this instead:

IndexIgnore *

RewriteEngine On  
RewriteBase /

# HTTP to HTTPS (including subdomains)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Canonical non-www to www redirect for main domain only
RewriteCond %{HTTP_HOST} ^(example\.com)\.?$ [NC]  
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# Remove ".php" extension on requested URL
RewriteCond %{THE_REQUEST} \s/(.+)\.php[\s?] [NC]  
RewriteRule ^ /%1 [R=301,L,NE]  

# Internally rewrite request to append ".php" extension
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{DOCUMENT_ROOT}/$1.php -f  
RewriteRule ^(.+?)/?$ $1.php [L]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.