代码之家  ›  专栏  ›  技术社区  ›  cespon Tom

Laravel强制www和HTTPS错误:重定向到索引。php

  •  1
  • cespon Tom  · 技术社区  · 6 年前

    我有以下几点 .htaccess 对于我的Laravel应用程序:

    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点,但我找不到

    1 回复  |  直到 6 年前
        1
  •  3
  •   cespon Tom    6 年前

    改变指令的顺序是解决方案:

    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>