Laravel尾部斜杠重定向到本地主机

17

当我尝试这个时

http://localhost/Testlaravel/public/users/login

它可以工作。但当我尝试时

http://localhost/Testlaravel/public/users/login/ 

它将我重定向到

http://localhost/users/login/

有什么想法呢?

这是我的htaccess文件

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
4个回答

22

把代码改成这样:

Options -MultiViews
RewriteEngine On
RewriteBase /Testlaravel/public/

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]

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

我尝试了一下,但现在它重定向到http://localhost/C:/xampp/htdocs/Testlaravel/public/users/login。 - user3213240
好的,我添加了 RewriteBase /Testlaravel/public/ 然后现在它可以工作了,谢谢 :) - user3213240
1
对我来说,这行代码:RewriteBase /Testlaravel/ 起了作用,因为我没有使用 public 文件夹并将其删除了。谢谢。 - shasi kanth
@kupendra:每个人的问题都不同,这并不是解决你问题的答案。请发布一个新的问题,我们会研究它。 - anubhava
它对我来说重定向到了正确的位置,但现在我得到了500错误页面。 - sr9yar
显示剩余2条评论

1
如果上述方法不起作用,则清除浏览器历史记录和缓存,然后再尝试。

0

我的项目目录是

c:\wamp\www\laravel_apps\li3\

并且我已经修改了public文件夹内的.htaccess文件,内容如下:

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

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /laravel_apps/li3/public/$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>

我刚刚替换了

RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteRule ^(.*)/$ /laravel_apps/li3/public/$1 [L,R=301]

现在,如果您在URL末尾添加一个斜杠,它将重定向到同一页。

0

我刚刚将这个添加到我的 .htaccess 文件中,它运行得很好!

RewriteCond %{REQUEST_URI} !^

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接