保护基于查询字符串参数与HTTP验证的URL字符串、参数、URL、HTTP

2023-09-02 09:51:46 作者:Betray

我有一个网站,像这样的链接:

I have a site with links like this:

http://www.example.com/index.php?id=1

http://www.example.com/index.php?id=3

等。

我想有htaccess的密码保护特定ID,说200。

I would like to have htaccess password protection for a specific ID, say 200.

我怎样才能做到这一点?

How can I do this?

推荐答案

这是不是直接的,但这里是一个方式,它可以在的.htaccess 做自己:

This is not straight forward but here is a way it can be done in .htaccess itself:

RewriteEngine On

# set URI to /index.php/200 if query string is id=200
RewriteCond %{QUERY_STRING} (?:^|&)id=(200|1)(?:&|$) [NC]
RewriteRule ^(index.php)/?$ $1/%1 [NC]

# set SECURED var to 1 if URI is /index.php/200
SetEnvIfNoCase Request_URI "^/index.php/(200|1)" SECURED

# enforce auth if SECURED=1
AuthType Basic
AuthName "Login Required"
AuthUserFile /full/path/to/passwords
Require valid-user
Order allow,deny
Allow from all
Deny from env=SECURED
Satisfy any