0

我正在尝试重写不存在的 URL 以使用我的 index.php 文件,同时还将 HTTP:Authorization 环境变量附加到我的脚本中。到目前为止,我一次只能让一个或另一个工作。不是都。有人可以告诉我.htaccess 中的错误在哪里吗?

RewriteEngine on

# Get HTTP authorization
RewriteCond %{HTTP:Authorization} ^Basic.*

# Append to URL
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L]

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L]
4

2 回答 2

1

首先,您的 .htaccess 在我的测试环境中运行良好。我得到_auth每个请求的论点。

但你不需要所有这些RewriteCond,只需要一个RewriteRule

RewriteRule .* index.php?_auth=%{HTTP:Authorization} [QSA,L]

这会将所有请求重写为index.php,并将授权标头添加为_auth参数。

如果您只想使用_auth参数重写不存在的 URL,只需将RewriteConds 添加到RewriteRule. RewriteCond HTTP:Authorization如果在您的情况下缓存是一个问题,请添加

RewriteEngine on

RewriteCond %{HTTP:Authorization} ^Basic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?_auth=%{HTTP:Authorization} [QSA,L]
于 2013-03-07T22:18:09.290 回答
0

我很确定 Authorization 标头可以通过 PHP 访问,因此似乎没有必要将该值附加为查询参数。

查看$_SERVER超全局变量,也许关键AUTH_TYPE就是您要找的?

<?php $_SERVER['AUTH_TYPE']) ?>

无论如何,如果您仍然坚持使用 .htaccess 解决方案,那么您实际上可以将整个第一条规则全部废弃;如果存在,_auth 参数将附加 Authorization 标头。

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L]
于 2013-03-07T22:52:31.213 回答