301使用正确的段重定向

Redirect all changed/duplicate urls

/changed-title/2
/another-changed-title/2

To correct url

/original-correct-title/2

For example

http://stackoverflow.com/questions/232323/original-title-of-question

If I change the last part of url (slug) and hit enter

http://stackoverflow.com/questions/232323/changed-title-of-question-duplicate

still gets redirected to correct url with correct slug

Try it on the current page

I want to do the same thing

I am using symfony framework

routing.yml

topic_item_redirect:
    path:   /topic/{title}/{id}
    defaults: { _controller: AppBundle:Topic:redirectToItem }

topic_item:
    path:    /topic/{title}/{id}
    defaults: { _controller: AppBundle:Topic:item }

TopicController.php

public function redirectToItemAction($title,$id) {

    $title = $this->getDoctrine()->getManager()->getConnection()
        ->fetchColumn(
                'select title from topic where id = ?',
                [$id]);

    // action which renders topic
    return $this->redirectToRoute('topic_item',['title'=>$title,'id'=>$id],301);
}

This approach not working for me

To simulate SO behavior you can add this kind of php snippet in TopicController.php:

$id    = $_GET['id'];
$title = $_GET['title'];

$dbTitle = $this->getDoctrine()->getManager()->getConnection()
    ->fetchColumn(
            'select title from topic where id = ?',
            [$id]);

if ($title != $dbTitle) {
   // redirect with 301 to correct /questions/<id>/<title> page
   header ('HTTP/1.1 301 Moved Permanently');
   header('Location: /questions/' . $id . '/' . $dbTitle);
   exit;
}
// rest of your script

Since both topic_item_redirect and topic_item use the same path attribute, you will most certainly get redirected every time.

I think you will need to handle this in your controller: Get the slug, query the DB and decide whether you need/need not to redirect.

Hope this helps