Laravel: Redirect to https://www.*** does not work

2019-09-20 16:18发布

问题:

I added to the .htaccess file this code https://stackoverflow.com/a/13997498/2311074 to redirect my website always to https://www.**********. This works in Chrome, but it is not working in Firefox.

If I enter

https://example.com

then its not redirecting anywhere.

in Firefox. In Chrome they are correctly redirected to https://www.example.com.

I aready deleted the cache and tried to call the website from other PC's but its still not working. This is my htaccess FIle from Laravel:

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

    RewriteEngine On


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

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

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

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    # [NC] is a case-insensitive match
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

What am I doing wrong?

回答1:

The problem was that I only had a SSL certificate for the domain www.example.org but not a SSL certificate for the domain example.org. Thus the url https://example.org is invalid, was not loaded and could not be redirected with the .htaccess file.



回答2:

See comments below for the answer to question.