0

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>
seeker
  • 61
  • 1
  • 11
  • Please read the documentation of the tools you use. The ``RewriteRule`` command operates on a _path_, not a _url_. So it will never match if your pattern starts with the "domain name". – arkascha May 25 '18 at 09:42
  • @arkascha I changed to a path, but it still doesn't work. I edited the question to be as you said. – seeker May 25 '18 at 14:43

1 Answers1

1

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).

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • thank you for your answer. 1- About your remark, I'm currently using shared hosting, and the host provider doesn't let me use httpd.conf. – seeker May 26 '18 at 08:23
  • 2- Your code solved for 2 addons, but it also caused all direct "addon.com" clicks to go to primary.com, so I also added 'RewriteCond %{HTTP_HOST} (.*)primary\.com$ '. – seeker May 26 '18 at 08:23
  • 3- About the other 3 addons, they also have their own .htaccess, but they only have 'code'RewriteRule ^(js|css)..'code', I red the [documentation][1] so I expected that they are independent and both work in sequence, why does it override an independent RewriteRule in root level .htaccess? [1]: http://httpd.apache.org/docs/2.2/howto/htaccess.html – seeker May 26 '18 at 08:24
  • OK, I added your suggestion for the `RewriteCond` in slightly altered form to the answer. A great improvement, I did not think of that. – arkascha May 26 '18 at 08:30
  • About your comment 3.: I do not really understand what the issue is. Certainly _all_ relevant dynamic configuration files are considered, starting from the `DOCUMENT_ROOT` folder iterating up to the folder matching the request path. That is one of the reasons why such files slow down the http server... Rules are applied in the order they occur. Later rules definitely do _not_ "override" earlier rules. But keep in mind that if you access `http://addon1.com/css/file.css`, then the to plevel `.htaccess` file (yours) is _not_ considered, since it is _not_ in that hosts `DOCUMENT_ROOT`. – arkascha May 26 '18 at 08:34
  • Sure, I understood that much and altered the answer accordingly. I said so above. – arkascha May 26 '18 at 08:36
  • In 3, it seems that when `primary.com/addon1.com` clicked, it reads any rewrite rule in addon1's htaccess, and it ignores our above code in primary htaccess, so fails to redirect `primary.com`, but it shows `primary.com/addon1.com/index.html` I didn't understand why.. – seeker May 26 '18 at 09:16
  • Not sure what is going on. I altered the `RewriteCond` once more, please update your code. And make sure this is not a caching issue, so test clearing your browsers cache or use a new anonymous browsing instance of the browser. – arkascha May 26 '18 at 09:24
  • No. Unfortunately, it didn't work for addons with css rewrite rules in its own htaccess. I tried with Firefox new private window and Chrome with clearing all past, but failed. – seeker May 26 '18 at 15:47