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

Limit Access to Apache Website with specific Header

· 52CE 我爱测

I only want to make my website accessible if a certain header (auth-token) with a certain value (test) is transmitted. For this I have already created a .htaccess file and added lower code to it. The problem is that the website c…

问题原文

原帖链接 · Hans Meier · CC BY-SA 4.0

I only want to make my website accessible if a certain header (auth-token) with a certain value (test) is transmitted. For this I have already created a .htaccess file and added lower code to it.

The problem is that the website can no longer be reached at all, even if the corresponding header is given. It alswas says that i have no Permission to Access this Server.

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"

RewriteEngine on
RewriteCond %{HTTP:auth-token} ^test$
RewriteRule .? - [E=headerok]

Order allow,deny
Deny from all
Allow from env=headerok

This is what the Chrome Developer Console responses:

I know that this kind of protection is not really secure, but for my purposes this is sufficient.

Can someone please help me with this problem?

已采纳解答

原帖链接 · Dave Sherohman · CC BY-SA 4.0

If you're using apache 2.4, you should consider using the new (Require) access control methods instead of the 2.0/2.2 (Allow/Deny) methods. 2.4 provides a backwards compatibility module for now, but I expect that to go away at some point.

The Apache 2.4 access control documentation suggests that you should be able to accomplish this with:

<If "%{HTTP:auth-token} in { 'test' }">
    Require all granted
</If>
<Else>
    Require all denied
</Else>

There's also an example on that page using mod_rewrite which is more similar to the approach in the question, but:

The <If> directive, added in 2.4, replaces many things that mod_rewrite has traditionally been used to do, and you should probably look there first before resorting to mod_rewrite.

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

资料来源与适用范围

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

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

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