Header 没有设置 RewriteRule [R=302,L]
Header not being set with RewriteRule [R=302,L]
目标: 我正在尝试通过 htaccess 设置两个 headers:
X-Robots-Tag: noindex, nofollow
Location: http://example.com/foo
PoC: 在 PHP 中可以这样做,效果很好:
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
问题: 在我的 .htaccess
文件中我有这个:
# Do not let robots index anything from /out/
RewriteCond %{REQUEST_URI} ^/?out/?
Header set X-Robots-Tag "noindex, nofollow"
...
# Redirect /out/example/ type links
RewriteRule ^/?out/example/(.*)$ "http://example.com/" [R=302,L]
我确定有一个我没有看到的简单错误,但是如果我检查 http://localhost/out/example/foo 的 header,Location
header 已设置,但 X-Robots-Tag
未设置。
HTTP/1.1 302 Found
Date: Wed, 08 Jun 2016 23:59:18 GMT
Content-Type: text/html; charset=iso-8859-1
Transfer-Encoding: chunked
Connection: close
Location: http://example.com/foo
...
但是,触发 404(例如 http://localhost/out/404)将设置适当的 header:
HTTP/1.1 404 Not·Found
Date: Wed, 08 Jun 2016 23:56:19 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding,User-Agent
X-Robots-Tag: noindex, nofollow <--- set
...
问题出在哪里?
Apache 只会为 success/2xx 响应代码设置 headers。为了为任何其他状态代码设置 header,您需要使用 always
关键字:
Header always set X-Robots-Tag "noindex, nofollow"
When your action is a function of an existing header, you may need to specify a condition of always
, depending on which internal table the original header was set in. The table that corresponds to always
is used for locally generated error responses as well as successful responses. Note also that repeating this directive with both conditions makes sense in some scenarios because always
is not a superset of onsuccess
with respect to existing headers:
- You're adding a header to a locally generated non-success (non-2xx) response, such as a redirect, in which case only the table corresponding to always is used in the ultimate response.
- You're modifying or removing a header generated by a CGI script, in which case the CGI scripts are in the table corresponding to always and not in the default table.
- You're modifying or removing a header generated by some piece of the server but that header is not being found by the default onsuccess condition.
解决方案是执行以下操作:
# Redirect /out/example/ type links
RewriteRule ^/?out/example/(.*)$ "http://example.com/" [R=302,L,E=OUTLINK:1]
# Add the robots header if E was set above
Header always set X-Robots-Tag "noindex, nofollow" env=OUTLINK
注意: 这是一个挑战,因为最初的解决方案是将 "noindex" header 添加到导致我的站点失效的所有内容。我希望这对以后的人有所帮助。
目标: 我正在尝试通过 htaccess 设置两个 headers:
X-Robots-Tag: noindex, nofollow
Location: http://example.com/foo
PoC: 在 PHP 中可以这样做,效果很好:
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
问题: 在我的 .htaccess
文件中我有这个:
# Do not let robots index anything from /out/
RewriteCond %{REQUEST_URI} ^/?out/?
Header set X-Robots-Tag "noindex, nofollow"
...
# Redirect /out/example/ type links
RewriteRule ^/?out/example/(.*)$ "http://example.com/" [R=302,L]
我确定有一个我没有看到的简单错误,但是如果我检查 http://localhost/out/example/foo 的 header,Location
header 已设置,但 X-Robots-Tag
未设置。
HTTP/1.1 302 Found
Date: Wed, 08 Jun 2016 23:59:18 GMT
Content-Type: text/html; charset=iso-8859-1
Transfer-Encoding: chunked
Connection: close
Location: http://example.com/foo
...
但是,触发 404(例如 http://localhost/out/404)将设置适当的 header:
HTTP/1.1 404 Not·Found
Date: Wed, 08 Jun 2016 23:56:19 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding,User-Agent
X-Robots-Tag: noindex, nofollow <--- set
...
问题出在哪里?
Apache 只会为 success/2xx 响应代码设置 headers。为了为任何其他状态代码设置 header,您需要使用 always
关键字:
Header always set X-Robots-Tag "noindex, nofollow"
When your action is a function of an existing header, you may need to specify a condition of
always
, depending on which internal table the original header was set in. The table that corresponds toalways
is used for locally generated error responses as well as successful responses. Note also that repeating this directive with both conditions makes sense in some scenarios becausealways
is not a superset ofonsuccess
with respect to existing headers:
- You're adding a header to a locally generated non-success (non-2xx) response, such as a redirect, in which case only the table corresponding to always is used in the ultimate response.
- You're modifying or removing a header generated by a CGI script, in which case the CGI scripts are in the table corresponding to always and not in the default table.
- You're modifying or removing a header generated by some piece of the server but that header is not being found by the default onsuccess condition.
解决方案是执行以下操作:
# Redirect /out/example/ type links
RewriteRule ^/?out/example/(.*)$ "http://example.com/" [R=302,L,E=OUTLINK:1]
# Add the robots header if E was set above
Header always set X-Robots-Tag "noindex, nofollow" env=OUTLINK
注意: 这是一个挑战,因为最初的解决方案是将 "noindex" header 添加到导致我的站点失效的所有内容。我希望这对以后的人有所帮助。