Search code examples
apache.htaccesshttp-redirectmod-rewrite

RewriteCond [.htaccess]


I would like to know why is this configuration returning me a 308 when requesting to /api/client via http (port 80).

Note: The device that receive the response is an ARDUINO, he doesnt handle the 3XX responses.

    RewriteCond %{REQUEST_URI} !='/api/client'                    # ....
    #RewriteCond expr "%{REQUEST_URI} -strmatch '/api/client'"    #Doesn't work
    #RewriteCond expr "%{REQUEST_URI} !~/api/"                    #Doesn't work
    #RewriteCond !%{REQUEST_URI} ^/api/client                     #Doesn't work
    #RewriteCond %{REQUEST_URI} !^/api/client                     #Doesn't work

    RewriteCond %{SERVER_PORT} !=443
    RewriteRule ^(.*)$ https://example.com/$1 [R=308,L]

The objetive is to redirect all the HTTP request (Port 80) to HTTPS except the requests to /api/client


Solution

  • The objective is to redirect all the HTTP request (Port 80) to HTTPS except the requests to /api/client

    Try this rule with THE_REQUEST variable:

    RewriteEngine On
    
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{THE_REQUEST} !/api/client [NC]
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=308,L]
    

    THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

    Make sure to test it after clearing old cache or from a new browser.