简体   繁体   English

如何重定向。 旧网址转换为新网址。 [Laravel,htacces]

[英]how to redirect. old url to new url. [Laravel, htacces]

how to redirect old url to new url. 如何将旧网址重定向到新网址。

old url: http://www.example.com/main.php?id=111 旧网址: http://www.example.com/main.php?id=111http://www.example.com/main.php?id=111

new url: http://www.example.com/n/111 新网址: http://www.example.com/n/111http://www.example.com/n/111

my solution is: in routes.php 我的解决方案是:在routes.php中

Route::get('/main.php?id={id}', array('uses' => 'App\Controllers\Front\PageController@oldToNew'));

in action: 实际上:

public function oldToNew($id)
{
return Redirect::to('http://www.example.com/n/'.$id);
} 

but this code wont work. 但是此代码无法正常工作。 pls help. 请帮助。

You could make a route that catches all routes that are not in your routes.php. 您可以创建一条路由,以捕获不在route.php中的所有路由。 It has to be in the bottom of your routes.php file. 它必须在routes.php文件的底部。

Here you can check if id exists and if the uri contains main.php. 在这里,您可以检查id是否存在以及uri是否包含main.php。

Route::get('{uri}', function($uri)
{
    $id = Input::get('id');

    if(preg_match('/main.php/i', $uri) && isset($id)){
        return Redirect::to('http://www.example.com/n/'.$id);
    }else{
        App::abort(404);
    }

})->where('all', '.*');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM