简体   繁体   中英

Set Header using REQUEST_URI and ap_expr in Apache 2.4

I need to set a noindex header for our sitemap.xml page. I can't use a <meta> tag, so the other option seems to involve setting a header via Apache.

Additionally, I can't use the <Files> directive because our sitemap.xml is generated by PHP and is not a file.

I've tried this, but I don't think RewriteCond works in conjunction with Header because this is affecting all pages:

RewriteCond %{REQUEST_URI} ^/sitemap\.xml$ [NC]
Header set X-Robots-Tag "noindex"

This doesn't produce any effect:

<If "%{REQUEST_URI} =~ /^sitemap.xml$/">
    Header set X-Robots-Tag "noindex"
</If>

It seems like the best option would be to use ap_expr , but this doesn't work either:

Header set X-Robots-Tag "noindex" "expr=%{REQUEST_URI} =~ m#^/sitemap\.xml$#i"

Can someone spot a regex problem? Am I misunderstanding how one or more of these techniques should be used?

For what it's worth, we prefer to version control our .htaccess files and therefore don't put config items like these in httpd.conf or vhost etc.

<If "%{REQUEST_URI} == '/sitemap.xml'">
    Header set X-Robots-Tag "noindex"
</If>

or

<If "%{THE_REQUEST} =~ m#\s/sitemap\.xml# ">
     Header set X-Robots-Tag "noindex"
</If>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM