Htaccess for Wordpess set on single subdomain

My suggestion is: use the same standard .htaccess configuration for WordPress on a single domain in each WordPress directory.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Along with it, add a small CODE to make sure users are redirected to the correct domain in case they reach to that subdirectory using another domain:

RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule ^(.*)$ "https://domain.com/$1" [R=301,L]

After that, your .htaccess for domain.com will be like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule ^(.*)$ "https://domain.com/$1" [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

and for subdomain.domain.com, it’ll be like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$
RewriteRule ^(.*)$ "https://subdomain.domain.com/$1" [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Reasons:

  1. Using standard CODE is recommended because it’s easier to maintain & debug, as it’s same everywhere.

  2. The extra two line are recommended mainly for SEO. Without them, one of your domain may be accessible using another domain. That may raise some duplicate content question.

Leave a Comment