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 ...

Apache Version : 2.4


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
  • Hi Freddy, Thanks for the post. this isn't working , both the options[wget] are taking me to SSL page and more over Error Log is not appending with rewrite Log. – Rajesh Jul 12 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.