Tim Ogilvy
Tim Ogilvy

Reputation: 1973

.htaccess rewrite rule for anything except specific http_host

Context of the problem:

I'm using shared hosting via cPanel, and on my current hosting setup I don't have the ability to create additional virtual hosts. Presently to keep costs down I am trying to avoid more complexity, so I have several sites symlinked in to public_html

 public_html /
 |
 |---> siteone.net /
 |
 |---> sitetwo.com /
 |
 |---> sitethree.net.au /
 |
 |--  (etc)

.

The Problem:

'sitetwo.com' is accessible from siteone.net as http://siteone.net/sitetwo.com/

.

Aim for Solution:

My goal is to prevent access to the site folders for the other sites. I'm only simulating virtual hosts which is clearly not ideal, but I need to do it for a few more months.

NB: Currently domains are routed using htaccess code as follows:

 RewriteCond %{HTTP_HOST} ^(www.)?siteone.net$
 RewriteCond %{REQUEST_URI} !^/siteone/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /siteone/$1
 RewriteCond %{HTTP_HOST} ^(www.)?siteone.net$
 RewriteRule ^(/)?$ siteone/index.php [L]

.

Attempted Solution:

My aim was to redirect anything that was not routed via siteone.com one to 404:

 RewriteCond %{HTTP_HOST} !^(www.)?siteone.net$
 RewriteCond %{REQUEST_URI} ^/siteone.net/
 RewriteRule -- serve 404 page ----- [L]

This did not work. The mod_rewrite manual and google don't seem to think that NOT logic is available against HTTP_HOST in a rewrite condition... or else I'm not reading it right.

Any assistance will be gratefully received.

Upvotes: 3

Views: 3411

Answers (1)

anubhava
anubhava

Reputation: 785146

Ok add this code as first rule in /siteone.net/.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?siteone\.net$ [NC]
RewriteRule ^ - [F]

Optionally if .htaccess doesn't exist for sitetwo.com then use:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www.)?sitetwo\.com$ [NC]
RewriteRule ^sitetwo\.com/ - [NC,F]

Upvotes: 2

Related Questions