I have multiple domains hosted within the same directory on a server, and each redirects to a respective subdirectory. I am also redirecting all domains to HTTPS and removing WWW via an .htaccess file. It all works as is, but the code feels clunky and I'm sure it could be simplified. Any tips on how to do so, how to remove anything redundant, would be appreciated.
Seems to me that I should be able to handle the HTTPS and non-WWW parts universally, and then only need individualization for the domain-to-subdirectory parts. Note that I also redirect variations of one domain (.com and .net to a .org address), but I presume there's not any simplification that can happen there.
Here is enough code to illustrate everything that's happening in my busy little .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
RewriteCond %{HTTP_HOST} ^www\.domainone\.org [NC]
RewriteRule (.*) https://domainone.org/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(ww+\.)?domainone\.org
RewriteCond %{REQUEST_URI} !/domainone-org/
RewriteRule ^(.*)$ /domainone-org/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule (.*) https://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(ww+\.)?domaintwo\.com
RewriteCond %{REQUEST_URI} !/domaintwo-com/
RewriteRule ^(.*)$ /domaintwo-com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domainthree\.net$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domainthree\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainthree\.org [NC]
RewriteRule (.*) https://domainthree.org/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(ww+\.)?domainthree\.org
RewriteCond %{REQUEST_URI} !/domainthree-org/
RewriteRule ^(.*)$ /domainthree-org/$1 [L]
I couldn't get the RewriteCond %{HTTP_HOST} ^www\. [NC] to work in the IfModule section. That's why there is what seems like redundant code below that for each domain:
RewriteCond %{HTTP_HOST} ^www\.domainone\.org [NC]
RewriteRule (.*) https://domainone.org/$1 [L,R=301]
If I try to handle the HTTPS and the non-WWW as two separate rewrites in the IfModule (see code below), then the redirects break, and I get a "too many redirects" error in Google Chrome window.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thank you for your help.