david
david

Reputation: 273

Simplifying an .htaccess file with several RewriteCond's and RewriteRule's across multiple domains

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.

Upvotes: 1

Views: 134

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133610

Please make sure to place these Rules at top of your .htaccess rule file. With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

Options +FollowSymLinks
RewriteEngine on

# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

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

</IfModule>


In case you want to perform https and non-www only for specific domains then try following. Please use either above or following rules set only at a time.

Options +FollowSymLinks
RewriteEngine on

# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^(?:www\.)?(domainone\.org|domaintwo\.com|domainthree(?:\.net|\.com))$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

  RewriteCond %{HTTP_HOST} ^(?:www\.)?(domainone\.org|domaintwo\.com|domainthree(?:\.net|\.com))$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

</IfModule>

Explanation: Adding detailed explanation for above.

^(?:www\.)?         ##Matching from starting of the value, where in a non-capturing group  matching www followed by DOT keeping it optional here.
(                   ##Starting a capturing group here.
 domainone\.org     ##Matching domainone\.org here.
 |                  ##OR condition to match either this or further values.
 domaintwo\.com     ##Matching domaintwo\.com here.
 |                  ##OR condition to match either this or further values.
 domainthree        ##Matching domainthree here.
   (?:              ##Starting a non-capturing group here.
      \.net|\.com   ##Matching dot net OR dot com here.
   )                ##Closing non-capturing group here.
)$                  ##Closing capturing group here.

Upvotes: 2

Related Questions