VD với Java (sử dụng Spring)
...
@GetMapping("/phim/{name}")
public String getFilmSummary(@PathVariable String name, ModelMap model) {
//nanatsu-no-taizai-imashime-no-fukkatsu-f1663 -> f1663
// -f1663 -> f1663
// f1663 -> null
String id = name.substring(name.lastIndexOf("-") , name.length()); // Cắt chuỗi từ "-" cuối cùng đến hết chuỗi
String fullName = filmRepository.findFullNameById(id);
// Kiểm tra fullName
// Nếu fullName null thì trả về view "not-found"
// Nếu fullName giống name thì lấy dữ liệu film, đổ vào view "film-details" rồi trả về client
// Nếu fullName khác name thì redirect url có fullName
if(fullName == null {
// url .../phim/f1663.html sẽ ra not found
return "not-found";
} else if(fullName.equals(name)) {
// url .../phim/nanatsu-no-taizai-imashime-no-fukkatsu-f1663.html sẽ lấy dữ liệu phim
// Lấy dữ liệu film
Film film = filmRepository.findByFullName(name);
// Gán film vào view
model.addAttribute("film", film);
return "film-details";
} else {
// url .../phim/-f1663.html sẽ redirect về url .../phim/nanatsu-no-taizai-imashime-no-fukkatsu-f1663.html
return "redirect:/phim/" + fullName; // redirect về "/phim/nanatsu-no-taizai-imashime-no-fukkatsu-f1663"
}
}
Mình xài Java có thể không đúng công nghệ với trang animehay hoặc không đúng ý bạn, tuy nhiên tóm lại thì việc rediect do backend làm, bạn phải hiểu backend thì mới áp dụng được. Chủ yếu là so sánh chuỗi của request và dữ liệu trên db, nếu lấy đúng thì trả về client, không đúng cấu trúc nhưng có id thì lấy đúng cấu trúc rồi trả về view, sai hoàn toàn cấu trúc thì ra not found
[spoiler]Gõ điện thoại mệt vcl
[/spoiler]