We have https://example1.com/login
and example2.com/login
being hosted from the same apache server (2.2.22). I want to restrict /login
on example1.com
.
example1.com/login
--> 404 (preferably) or 403example2.com/login
--> login page
I tried
<Location /login>
Order Deny,Allow
Deny from example1.com
Allow from example2.com
</Location>
It disables /login
from both example1.com
and example2.com
. Can I restrict just example1.com
?
You can do this using mod_rewrite which allows you to check the host through which the site is being accessed. Try the following, in your server config (or
<Directory>
section or.htaccess
file):The above serves a 404 Not Found if you request
example1.com/login
, but will not do anything if you requestexample2.com/login
(the request will simply be allowed through).The
=
prefix on theRewriteCond
CondPattern makes it an exact lexicographical comparison, not a regex, so no need to escape the dot or use anchors.In order to serve a 403 Forbidden instead you can change the flags on the
RewriteRule
, for example:I'm rather curious... this would seemingly allow all access, not deny it? The
Deny from ...
directive denies access to the user fromexample1.com
(via reverse DNS lookup on the remote IP address), not the user who is accessing theexample1.com
host. Neither theDeny
orAllow
directives should match so it should default Allow. (?)UPDATE: Note that
RewriteCond
directives (ie. conditions) apply to the singleRewriteRule
directive that follows. Continuing the above example, if you wish to block several URLS (eg./login
,/demo
and/test
) forexample1.com
only then you can either combine all these in theRewriteRule
pattern. For example:This is OK in this instance since the URLs are limited and all very short. Alternatively, you can create a condition for each URL and use the
OR
flag. For example:This states... for all URLs where the
HTTP_HOST
isexample1.com
and the URL is either/login
or/demo
or/test
then serve the 404 error document. However, the first example (using theRewriteRule
pattern) would be more efficient, since this is what's processed first.