Preserve HTTP/HTTPS protocol in .htaccess redirects

前端 未结 5 1594
北恋
北恋 2020-12-02 23:17

I have to redirect port 80 to 2368 in htaccess but I\'d like to keep the requested protocol intact so that SSL doesn\'t break.

I currently have this:



        
5条回答
  •  一整个雨季
    2020-12-02 23:30

    Or, you might use the solution posted as an answer by Jon Lin and rewrite it to use one RewriteCond and then set a variable as suggested in the answer by TMS.

    That would look like this:

    RewriteCond %{HTTPS}s ^(on(s)|offs)$
    RewriteRule ^ - [env=s:%2]
    
    RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
    RewriteRule ^ http%{ENV:s}://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]
    

    Or, if you prefer:

    RewriteCond %{HTTPS}s ^(on(s)|offs)$
    RewriteRule ^ - [env=proto:http%2]
    
    RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
    RewriteRule ^ %{ENV:proto}://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]
    

提交回复
热议问题