jarvis Posted March 15, 2018 Share Posted March 15, 2018 Hi All, I'm hoping someone can help as I'm new to regex and it's driving me mad. I have a bunch of URLs and it basically follows the format of: /employer/company-name/various-pages What I need to do, is redirect all the various-pages as they no longer exist. So the URL becomes: /employer/company-name/ The regex I have is as follows: \/employer\/(.+?)\/[a-z0-9\-]+\/ \/employer\/(.+?)\/ The issue I have, I need to make company-name a variable but do an exact match Any help is very much appreciated! Thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/306843-301-redirect-regex/ Share on other sites More sharing options...
gizmola Posted March 15, 2018 Share Posted March 15, 2018 (edited) How are you planning to do the redirect? With a 404 handler? In your site htaccess, let's assume you have this: errordocument 404 /404.php In your 404.php script you can start with some redirect code that parses the url to find a company-name you want to redirect for. There are a number of different ways to do this, but this one is commonly used: $urlpath = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); // You should have just the url path, ie. employer/company-name/foo... Now you can simply explode to get the pieces, no need for regex at all. Lastly you simply need to do whatever checking is appropriate to determine if you need to redirect or show a 404 page. This could be any weird url, so you'd have to flesh out the different possibilities and catch them (for example, what if $dirlist has only 1 entry? Otherwise it's pretty simple comparison logic: $companylist = array('company1-name', 'company2-name', etc.); $dirlist = explode('/', $urlpath); if ($dirlist[0] == 'employer') { if (in_array($dirlist[1], $companylist)) { header('HTTP/1.1 301 Moved Permanently'); header("Location: /employer/{$dirlist[1]}/"); exit(); } } // Follow this with your 404 Page markup Edited March 15, 2018 by gizmola Had a few php errors Quote Link to comment https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1557178 Share on other sites More sharing options...
fatkatie Posted July 20, 2018 Share Posted July 20, 2018 I'm not trying to undermine gizmola here. It's a fine answer. But since the topic of recognizing a name showed up, here's an interesting post: in_array vs strpos for performance in php. It concerns the fastest way to find that needle (within some constraints). https://stackoverflow.com/questions/21070691/in-array-vs-strpos-for-performance-in-php#21070786 If posting links are not welcome here, administrator, feel free to nuke the post. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1559850 Share on other sites More sharing options...
requinix Posted July 20, 2018 Share Posted July 20, 2018 This situation is a little more complex than just finding a needle in a haystack. There are slashes to consider. And the exact location of that needle. And the trailing slash after the company name. It's possible to do with just string functions, of course, but the result would be complex enough not to be worth it. Optimizing PHP code for microseconds is not worth it - PHP just isn't efficient enough for it to matter. Readability is almost always more important. Quote Link to comment https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1559858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.