Reputation: 637
Trying to use .htaccess
to set a header when on a specific page of the website.
I've tried various methods to set the header, but none of them result in the header being set in the response.
Method 1:
SetEnvIf Request_URI ^\/glossary\/$ glossary
Header Set Test-Header "success" env=glossary
I've also tried a simpler version where I try to match 'glossary' in any part of Request_URI
SetEnvIf Request_URI glossary glossary
Header Set Test-Header "success" env=glossary
Method 2:
<If "%{REQUEST_URI} == '/glossary/'">
Header Set Test-Header "success"
</If>
Method 3:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/glossary\/
RewriteRule ^ - [ENV=GLOSSARY]
Header Set Test-Header "success" env=GLOSSARY
I've also output Request_URI in a header in order to confirm that Request_URI has a value:
Header set Test-Output-Request_URI "%{Request_URI}e"
This results in the Header being set, as expected:
Test-Output-Request_URI: /glossary/
I'm not much of an expert in .htaccess, nor regex, but normally I'm able to plug away and find a solution. Not this time though. From what I've read, it seems like all of these methods should work, but I'm obviously missing something.
This is Apache 2.4.35
Not sure if it makes a difference, but this is a WordPress site.
Upvotes: 2
Views: 2180
Reputation: 785481
This should work for you in Apache 2.4+
<If "%{THE_REQUEST} =~ m#/glossary#">
Header Set Test-Header "success"
</If>
Instead of ==
operator for string comparison, we are using =~
operator for regex matching here.
Upvotes: 4