.htaccess隐藏共享主机上多个域的.php

I have one hosting service that houses my main domain and several other domains (not sub domains - they are their own websites). I want to be able to use the below code for any domain within this main directory.

Currently the folder structure is like this:

Main Domain -- (maindirectory/main domain website files). We'll call this www.maindomain.com

Other Domain -- (maindirectory/otherdomain/other domain website files). We'll call this www.otherdomain.com

The below code hides the .php file extension completely. So that even if a user were to enter in a link without the file extension it will still go to the specified page and still not show the .php

Obviously, this code works only for the main domain. However, I would really like to know how to alter it so that it can also work for the other domain.

I'm really hoping someone can please help me! :)

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.maindomain.com/$1 [R,L]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

try this one :

RewriteEngine On

RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php