dou6495 2017-12-05 10:18
浏览 286
已采纳

Laravel 5.4中的301重定向问题

I update the route of my category pages from /listing/category1 to /products/category1

Old Route:

Route::get('/listing/{slug}', 'ProductsController@listing');

New Route:

Route::get('/products/{slug}', 'ProductsController@listing');

Now I want to permanently redirect (301) my Old Routes to New Routes.

There are just 4-5 categories so I have added below code in .htaccess file in public folder to redirect all old URL's to New URL's manually with Redirect 301 script but it didn't work.

Redirect 301 /listing/category1 https://www.example.com/products/category1
Redirect 301 /listing/category2 https://www.example.com/products/category2

Please note website is having SSL certificate so links are starting with https.

There might be solution lying somewhere in Stack Overflow or some other website that I unable to found.

I tried adding below code in .htaccess file as well but of no use.

RewriteRule listing/category1 /products/category1 [R=301,L]
RewriteRule ^listing/([a-zA-Z0-9_-]+)$ products/category1

Complete code of .htaccess file is below:

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

    RewriteEngine On

    RewriteRule ^/?listing/([\w-]+)/?$ /products/$1 [L,NC,NE,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

So Please help me telling How to permanently redirect Old Routes to New Routes in Laravel 5.4?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doukong1897 2017-12-05 10:24
    关注

    This should work,

    Route::any('listing/{any?}', function ($any = null) {
        return Redirect::to('/products/'.$any, 301); 
    })->where('any', '.*');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传