![]() |
$routes->addRedirect with regex? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: $routes->addRedirect with regex? (/showthread.php?tid=78591) |
$routes->addRedirect with regex? - sneakyimp - 02-10-2021 I'm trying to deprecate the use of .html suffixes for my urls. I'm trying to redirect old urls like this: Code: https://www.example.com/subdir/some-item-title/page-specifier-15-1132.00.html Code: https://www.example.com/subdir/some-item-title/page-specifier-15-1132.00 NOTE: the some-item-title and page-specifier change and can be almost anything depending on the item. I added this addRedirect line to my Config/Routes.php file: PHP Code: $routes->addRedirect('subdir/([a-z\-]+)/([a-z\-]+)-(\d{2}-\d{4}\.\d{2})\.html', 'subdir/$1/$2-$3'); Code: https://www.example.com/subdir/$1/$2-$3 Does addRedirect not support backreferences? If so, it might be helpful to clarify this in the documentation. How should I go about solving this problem? NOTE: I do *not* want to redirect all urls ending in .html. RE: $routes->addRedirect with regex? - iRedds - 02-11-2021 The addRedirect() method does not support replacement. You can use this hack PHP Code: $routes->any('subdir/([a-z\-]+)/([a-z\-]+)-(\d{2}-\d{4}\.\d{2})\.html', function(){ Or create your own class that extends the Router class and implement a replacement in it. Or post a new issue on github or PR with fixes or comment in the documentation about the inability to use regex. RE: $routes->addRedirect with regex? - sneakyimp - 02-11-2021 (02-11-2021, 03:37 AM)iRedds Wrote: The addRedirect() method does not support replacement.Thanks for the clarification. Thanks also for the code. That hack is not pretty. RE: $routes->addRedirect with regex? - iRedds - 02-11-2021 (02-11-2021, 11:23 AM)sneakyimp Wrote: Thanks for the clarification. Thanks also for the code. That hack is not pretty.Yes, it looks ugly. The simplest, and probably better in this case, is to make a redirect at the web server level RE: $routes->addRedirect with regex? - sneakyimp - 02-15-2021 (02-11-2021, 11:36 PM)iRedds Wrote: The simplest, and probably better in this case, is to make a redirect at the web server level OK I think I will make a redirect method in my BaseController that works like the old CI3 redirect function.. Does anyone know why CI3 used the refresh method for Microsoft-IIS? Does anyone know why CI3 would check for HTTP/1.1 and then use 307 for GET requests and 303 for everything else? It looks like 303 and 307 were added for HTTP/1.1 and did not exist before. EDIT: I'm not sure, but it looks like a 307 redirect would encourage the client to re-POST any data that was posted. This does not sound like the behavior I'd be after. E.g., if someone's session had expired and they attempt to POST a form, I'd be redirecting them to a login page first, and it would not be ideal to have an immediate POST applied there. Also, it looks like we have HTTP/2 as of 2015, so we'll probably need a > comparison, and possibly additional logic. Any tips/hints/thoughts very much appreciated. RE: $routes->addRedirect with regex? - sneakyimp - 02-15-2021 I have added this function to my BaseController class. It's simpler than the CI3 function in a few ways, but works for apache 2.4.41. PHP Code: /** |