Preventing primary domain from showing addon domains' content as its subfolder

461 Views Asked by At

I have a primary domain and 5 addons as independent sites. Everything is fine, but when one clicks primary.com/addon1.com, s/he can access irrelevant content over primary website. I want to prevent this.

I am looking for a solution via the htaccess in root of primary, not to edit 5 htaccess files of addons.

Simply, I tried the below code in primary htaccess, but it doesn't work. I tried many other alternatives, but doesn't work. Why? What is wrong with this?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)primary\.com$
RewriteRule ^/(addon1|addon2|addon3|addon4|addon5)(.*)$ http://primary.com [R=301,L]
</ifModule>
1

There are 1 best solutions below

9
On

The documentation of the RewriteRule explains what section of the request URL the rules pattern is applied upon: a relative path section in case you use the rule in the context of a dynamic configuration file (which you do).

A relative path means: no leading slash (/), that would be an absolute path. This makes absolute sense once you start thinking about the logic of such dynamic configuration files.

So you could remove the leading slash from the pattern, but I consider it a better practice to make it optional. That way your rule will work regardless of where it is applied, in a dynamic configuration file or in the real host configuration:

RewriteEngine On
RewriteCond %{HTTP_HOST} (^|\.)primary\.com$
RewriteRule ^/?(addon1\.com|addon2\.com)(/|$) http://primary.com/ [R=301]

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug, they really slow down the http server, often for nothing and they can open pathways to security nightmares. They are only provided for situations where you do not have access to the real http servers host configuration (read: really cheap service providers).