大网数据广告活动
服务器运维问答

rewrite / restrict specific domain name in apache

· 52CE 我爱测

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 403 example2.com/login --> login …

问题原文

原帖链接 · akay · CC BY-SA 3.0

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 403

  • example2.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?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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

RewriteEngine On
RewriteCond %{HTTP_HOST} =example1.com [NC]
RewriteRule ^/?login$ - [R=404,L]

The above serves a 404 Not Found if you request example1.com/login, but will not do anything if you request example2.com/login (the request will simply be allowed through).

The = prefix on the RewriteCond 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:

RewriteRule ^/?login$ - [F]

<Location /login>
Order Deny,Allow
Deny from example1.com
Allow from example2.com
</Location>

It disables /login from both example1.com and example2.com.

I'm rather curious... this would seemingly allow all access, not deny it? The Deny from ... directive denies access to the user from example1.com (via reverse DNS lookup on the remote IP address), not the user who is accessing the example1.com host. Neither the Deny or Allow directives should match so it should default Allow. (?)


...how do i add multiple rewrite conditions for the same RewriteCond?

UPDATE: Note that RewriteCond directives (ie. conditions) apply to the single RewriteRule directive that follows. Continuing the above example, if you wish to block several URLS (eg. /login, /demo and /test) for example1.com only then you can either combine all these in the RewriteRule pattern. For example:

RewriteCond %{HTTP_HOST} =example1.com [NC]
RewriteRule ^/?(login|demo|test)$ - [R=404,L]

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:

RewriteCond %{HTTP_HOST} =example1.com [NC]
RewriteCond %{REQUEST_URI} ^/login$ [OR]
RewriteCond %{REQUEST_URI} ^/demo$ [OR]
RewriteCond %{REQUEST_URI} ^/test$
RewriteRule ^ - [R=404,L]

This states... for all URLs where the HTTP_HOST is example1.com and the URL is either /login or /demo or /test then serve the 404 error document. However, the first example (using the RewriteRule pattern) would be more efficient, since this is what's processed first.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。