登录

php - apache配置.htaccess使http转向https

现在网站http和https都可以访问,但希望访问的时候http直接跳转到https上去

这是我配置的.htaccess

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
#RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
</IfModule>

加入那两行后就打不开了,求解中~

网站:https://www.ineedtm.com

# PHP
大家讲道理大家讲道理2172 天前580 次浏览

全部回复(2) 我要回复

  • 迷茫

    迷茫2017-04-11 09:50:56

    这几天刚好在做同样的事情。

    看你的htaccess,应该是也使用了php框架或者整站软件了,我也用了。

    先上没有框架的,适合纯html和没有php路由的简单网站

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    </IfModule>

    有框架的也好办:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # BEGIN WordPress
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    把https改写放在前面,先把url从http改成https,之后再进行php框架的改写。

    回复
    0
  • 阿神

    阿神2017-04-11 09:50:56

    stackoverflow查了下,更改后下面就可以了。楼上的大家可以参阅下

    <IfModule mod_rewrite.c>
          Options +FollowSymlinks
          RewriteEngine On
    
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
         RewriteCond %{HTTPS} off
        RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>

    回复
    0
  • 取消回复发送