带有尾部斜杠的URL会重定向到localhost Laravel 5.1

时间:2015-12-03 14:52:28

标签: php .htaccess laravel-5 laravel-5.1 laravel-routing

我用http://localhost/project/women-fashion打了我的网址并且工作正常 但如果我访问http://localhost/project/women-fashion/,它会被重定向到http://localhost/women-fashion

我的路线:

Route::get('/{slug}/', 'SlugController@index')->where('slug', '[A-Za-z-0-9]+');

我的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 ^(.*)/$ /$1 [L,R=301]

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

如何,我可以在末尾将带有斜杠的url重定向到url而不用斜线? 谢谢,任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

我得到了答案。我刚刚添加了RewriteBase /project/remove / before the substitution RewriteRule ^(.*)/$ $1 [L,R=301]

正则表达式表示捕获(。*)从斜杠/ $开始^到结尾的所有内容,并用它找到的内容替换它。

基本上我不想在结果上添加斜线。

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

        RewriteEngine On
        RewriteBase /project/

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

参考文章:https://laracasts.com/discuss/channels/laravel/url-with-trailing-slashes-gets-redirected-to-localhost