How to redirect all HTTP requests to HTTPS

前端 未结 26 2161
小鲜肉
小鲜肉 2020-11-22 00:40

I\'m trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com) to HTTPS (https://www.example.com). I\'m using PHP btw.

26条回答
  •  广开言路
    2020-11-22 00:45

    Unless you need mod_rewrite for other things, using Apache core IF directive is cleaner & faster:

    
    Redirect permanent / https://yoursite.com/
    
    

    You can add more conditions to the IF directive, such as ensure a single canonical domain without the www prefix:

    
    Redirect permanent / https://myonetruesite.com/
    
    

    There's a lot of familiarity inertia in using mod_rewrite for everything, but see if this works for you.

    More info: https://httpd.apache.org/docs/2.4/mod/core.html#if

    To see it in action (try without www. or https://, or with .net instead of .com): https://nohodental.com/ (a site I'm working on).

提交回复
热议问题