【问题标题】:Laravel auth not working change mysite.com/index.php to mysite.comLaravel 身份验证不起作用将 mysite.com/index.php 更改为 mysite.com
【发布时间】:2017-10-15 11:12:28
【问题描述】:

我为开发设置了一个本地 Laravel 服务器,并通过 Git 将更改推送到我的生产服务器。

我做了一个新的 Laravel 项目,我认为它没问题。然后我在我的本地服务器上添加了 php artisan make:auth 并推送到生产但得到 404 找不到登录(或注册)。经过一番工作,我发现: www.mySite.com/login 找不到但如果我写 www.mySite.com/index.php/login 它可以工作。

所以现在我想我必须修改我的 htaccess 以忽略 index.php 但我无法让它工作。我尝试了一些建议,但似乎都不起作用,我不知道为什么它不起作用。

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

    RewriteEngine On
    RewriteBase Test/public/

*    RewriteCond %{THE_REQUEST} /index\.php [NC]           
*    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302, NC, NE]

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

标有 * 的两行是我在阅读 Stack Overflow 上的建议后尝试添加的(现在找不到链接)。我也试过 htaccess remove index.php from url 这个建议。

有什么想法吗?谢谢。 编辑: 我的 sites-enabled/000-default.conf 如下所示:

<VirtualHost *:80>


        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/Test/public

        <Directory /var/www/html/Test/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =nfoscan.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

而我的 000-default-le-ssl.conf 看起来像这样:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        # The ServerName directive sets the request scheme, hostname and port that


        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/Test/public



        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName mysite.com
</VirtualHost>
</IfModule>

我删除了配置中的 cmets..

我尝试添加

<Directory /var/www/html/Test/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>

到我的 000-default-le-ssl.conf 但它导致内部服务器错误..

【问题讨论】:

    标签: php apache laravel .htaccess authentication


    【解决方案1】:

    首先检查您的 apache 是否启用了 mod_rewrite,您可以通过以下方式轻松完成 输入

    apache2 -M
    

    甚至更简单:

    a2enmod rewrite
    

    如果您已经启用它,您将收到消息: "模块重写已启用"

    如果您还没有,那么您将收到消息: “启用模块重写”。您将不得不重新启动 apache。

    检查你是否有 htaccess 行(AllowOverride All)

    <Directory /var/www/html/project/>
      Options FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
    </Directory>
    

    【讨论】:

    • 嗨,我对 apache 和 apache 配置非常陌生。我用 apache 配置文件编辑了我的问题。我认为问题出在 000-default-le-ssl.conf 但我没有知道如何更改它以使其工作(请参阅我的编辑)。 /谢谢:)
    • 你在htaccess中有没有:SSLEngine on你也可以在apache日志中搜索错误:/var/log/apache2/error_log
    • 在我的 000-default-le-ssl.conf 中,我可以将 DocumentRoot 设置为 /var/www/html 或 /var/www/html/Test/public 两者都可以正常工作......但是当我弄乱 时,只有 有效,但 给出内部服务器错误???
    【解决方案2】:

    好的,我修好了。我的 .htaccess 文件中有一些问题。我从删除 /Test/public 中的 .htaccess 文件开始,然后在 000-Default-le-ssl.conf 文件中弄乱时我没有收到内部服务器错误。然后我只是复制了目录中的另一个 .htaccess 文件,一切正常:O

    感谢所有试图帮助我的人!

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # 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]
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    </IfModule>
    

    如果有人再次遇到此问题,请发布有效的 .htaccess 文件 :)

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 2013-04-17
      • 2016-04-05
      相关资源
      最近更新 更多