Apache 2.4 Rewrite rule based on Header value

apache-2.2apache-2.4Apache2

trying to do http to https based on header value of the request but failing some how.can some one help me here.

request is if a header value is xyz and it is equal to Y then serve http pages,for all other requests serve https pages. immediately i have another condition inside proxy module which has to be executed but it is failing to execute.
also unable to see Log traces for rewrite in error logs.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:xyz} !=Y [NC] --> if header contains xyz and its value is not equal to Y
    Rewrite ^/?(.*) https://%{SERVER_NME}/$1 [R,L] --> server https page.
    LogLevel alert mod_rewrite.c:trace3 --> Log the rewrite activity.

<IfModule mod_proxy_http.c> --> after above activity is completed continue this activity.
    RewriteCond ... 
    RewriteCond ...
    RewriteRule ...
<Ifmodule>
<IfModule>

Apache Version : 2.4

Best Answer

I only fixed your RewriteRule and added an additional protocol check. If you have different vhosts for HTTP and HTTPS, you can remove the protocol check. Just wanted to make sure there is no redirection loop if you're already on the HTTPS site.

RewriteEngine On
# make sure we only redirect if protocol is HTTP, remove if not needed
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP:xyz} !=Y [NC]
RewriteRule (.*) https://%{SERVER_NAME}$1 [R,L]

To check if the header works I used wget:

# redirect to HTTPS
wget -S -O - --header='xyz:foo' http://localhost

# stay on HTTP
wget -S -O - --header='xyz:y' http://localhost
Related Topic