HTTP到HTTPS重定向问题

时间:2015-06-23 06:50:41

标签: php apache https

我想将http请求重定向到https节点。

我试过了,

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

请在下面找到几个测试结果,我执行了,

http://sample.mydomane.co/ 重定向到 https://sample.mydomane.co/

//按预期工作

http://sample.mydomane.co/api-docs/ 未重定向到      https://sample.mydomane.co/api-docs/

//没有按预期工作

编辑:这是我的.htaccess文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    #Options +FollowSymLinks
    RewriteEngine On

    RewriteRule ^(api-docs|docs)($|/) - [L] # HERE IS THE PROBLEM I THINK

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # redirect all http -> https
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>

请告诉我如何实现或解决此问题。

2 个答案:

答案 0 :(得分:1)

我成功使用以下规则,注意正则表达式模式的区别

newString (*declared in the class*) == loadedString

答案 1 :(得分:0)

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

Apache Wiki

  

http://wiki.apache.org/httpd/RewriteHTTPToHTTPS