htaccess - Laravel强制https

时间:2017-10-10 13:18:34

标签: laravel .htaccess redirect

我的htacess文件重定向问题。

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


    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    #http > https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{REQUEST_URI} !/(api/list|api/categories)
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

我在启动méthod

中的AppServiceProvider中有这个
if(env('APP_ENV') == 'production'){
  \URL::forceScheme("https");
}

问题如下: 当我调用url myndd.fr时,我重定向到HTTPS即可。 但是当我打电话时

  • myndd.fr/api/list
  • myndd.fr/api/categories

我重定向到https://myndd.fr/index.php?params_url 对于这个特定的路线,我不想重定向到https。

有什么想法吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您必须排除index.php

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

    #http > https
    RewriteCond %{HTTPS} !=on [NC,OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https [NC]
    RewriteCond %{REQUEST_URI} !^/?index\.php$
    RewriteCond %{REQUEST_URI} !^/?(api/list|api/categories) [NC]
    RewriteRule ^.*$ https://%{HTTP_HOST}/%{REQUEST_URI} [R=301,L] 

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L] 

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

答案 1 :(得分:0)

我找到了解决方案:

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

    #http > https
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https [NC]
    RewriteCond %{REQUEST_URI} !^/?(api/list|api/categories) [NC]
    RewriteRule https://%{HTTP_HOST}/%{REQUEST_URI} [R=301,L]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>