【问题标题】:URL with trailing slashes gets redirected to localhost Laravel 5.1 [duplicate]带有斜杠的 URL 被重定向到 localhost Laravel 5.1 [重复]
【发布时间】:2016-03-08 05:57:04
【问题描述】:

我用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 吗? 谢谢,任何帮助表示赞赏。

【问题讨论】:

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


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2015-12-25
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2012-02-01
      相关资源
      最近更新 更多