Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/306843-301-redirect-regex/
Share on other sites

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 by gizmola
Had a few php errors
Link to comment
https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1557178
Share on other sites

  • 4 months later...

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.

Link to comment
https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1559850
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/306843-301-redirect-regex/#findComment-1559858
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.