Laravel: htaccess is ignored on homestead?

2019-08-04 17:48发布

问题:

I have created an Laravel app which I locally develop with homestead and which in online on a production server.

I only have a SSL certificate for www.mydomain.com but not for mydomain.com, thats why I force with htaccess a redirect to https://www.*****. I did this by changing the file myproject/public/.htaccess as explained in https://stackoverflow.com/a/13997498/2311074

The redirect works fine on my production server but there is no redirect happening on my local environment with homestead. Why is the redirect ignored on my local environment?

Here is the full .htaccess file:

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

    RewriteEngine On


    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]

    # 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]


</IfModule>

回答1:

.htaccess won't work with Homestead, because it uses nginx. .htaccess is for Apache.



回答2:

I had a similar problem. My production server uses Apache and I need mod_rewrite. Laravel even comes with .htaccess files out of the box!

Here's an elegant solution I found on here.

  1. Add the "type: apache" to my Homestead.yaml file.

    sites:

    map: mylocalapp.com

    to: /home/vagrant/code/public

    type: apache

  2. Reprovision vagrant to use Apache.

    vagrant up --provision

Solved from the answer here on Stack Overflow