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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办