【问题标题】:Laravel force www and HTTPS error: redirect to index.phpLaravel 强制 www 和 HTTPS 错误:重定向到 index.php
【发布时间】:2018-10-27 02:45:34
【问题描述】:

我的 Laravel 应用程序有以下 .htaccess

ServerSignature Off
Header always unset "X-Powered-By"

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

    RewriteEngine On

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

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

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

    # 4. Redirect http://www.example.com to https://www.example.com
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # 5. Redirect http(s)://example.com to https://www.example.com
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

我想强制使用 www. 域和 HTTPS。当我在浏览器中引入example.com/myurl时,服务器重定向到https://www.example.com/index.php而不是https://www.example.com/myurl

我认为问题出在第 4 点或第 5 点,但我找不到它

【问题讨论】:

    标签: laravel .htaccess https


    【解决方案1】:

    改变指令的顺序是解决办法:

    ServerSignature Off
    Header always unset "X-Powered-By"
    
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # redirect http://www.example.com to https://www.example.com
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^www\. [NC]
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        # redirect http(s)://example.com to https://www.example.com
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        # 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}]
    
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 1970-01-01
      • 2018-04-14
      • 2018-08-20
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      相关资源
      最近更新 更多