0

Good morning,

I am working on a site that will compare two things on each page dynamically from mysql through php. For examples sake, lets say its comparing travel destinations. An example url from one of these pages would be "http://sometravelwebsite.com/destinations/compare/New-York-City-vs-Tokyo"

The way the php is written, the order of the cities does not matter. If you enter

../New-York-City-vs-Tokyo

you will get the same information as if you entered

../Tokyo-vs-New-York-City 

The only difference is which city's information is on the left side, and which city's information is on the right.

For SEO purposes, it would be better if there was only one version of the page, with the other url redirecting to the main url (e.g. ../New-York-City-vs-Tokyo is the main url that is indexed by Google, but if the other url: ../Tokyo-vs-New-York-City is entered, it 301 redirects to the main url).

My question is: Can this be done dynamically?

The only thing I can think of is to use an autoincrementing index in the mysql database and always put the city with a lower index number on the left, and the city with a higher index number on the right.

That said, I would prefer if I had some control over which city was on which side of the page. While I'm not going to go through and decide which city goes on which side for EVERY comparison (which would get crazy the more cities that were added), I would like to have control over some of them in order to put the city with the more desirable characteristics on the left side of the page where it will be seen first (assuming individuals are reading left to right).

Can anyone think of a better way to do this dynamically through either php, mysql, .htaccess or any other way?

Any help would be greatly appreciated!

krishna
  • 4,069
  • 2
  • 29
  • 56
will74
  • 5
  • 2
  • This doesn't make sense, the dynamic PHP handles both but if they come from a search engine you want a redirect? Stick to a simple naming convention or always order them in alphabetically order or something that way you won't have separate variations. Im sure Google won't mind if its 'A Vs B' or 'B Vs A' im sure it would return both equally if useful to the searchee. – Matt The Ninja Mar 25 '14 at 12:51
  • "put the city with the more desirable characteristics on the left side of the page" If that's really what you want, then why are you worrying about the order of the cities in the url? Just pull the information for each queried city, determine which is more "desirable" using whatever metrics you've defined, and display them based on that ranking. – Patrick Q Mar 25 '14 at 12:53
  • Hi @MattTheNinja thanks for your reply. I apologize if I wasn't clear. I didn't mean that if they come from a search engine I want a redirect, just that I only want the search engine to index one of the pages. What I should have mentioned was that one way people get to these pages is through a form with two textboxes, so they could enter in their search as either New York City vs. Toyko or Tokyo vs. New York City. I guess it makes sense to have them appear in something like alphabetical order, but I'm thinking that the page would conver better if the city with more advantages was on the left – will74 Mar 25 '14 at 12:57
  • @PatrickQ, I am worrying about the order of the cities in the URL for SEO reasons. If someone were to share the page on social media, or link to the page somewhere else, it would be preferable that only one of the two possible variations be the one that is linked to. Perhaps I'm mistaken in that assertion, but that was what I was thinking. – will74 Mar 25 '14 at 13:04
  • @will74 You can still put better performing object on the left. Make the URL always alphabetically, put the 2 cities (or more) into array and sort alphabetically. As for displaying on the page, query your database and display them left to right based on the calculated 'score' of the one you think will convert higher. – Matt The Ninja Mar 25 '14 at 14:11
  • @MattTheNinja Thanks for your help! This is exactly what I did. If you format your last comment into an answer, I can select it as the accepted answer to this question. – will74 Apr 15 '14 at 13:55

2 Answers2

0

Create and array like

$allowed_left = array('New York City','Milan', 'London');

then test if the city1 is not allowed on the left and redirect if true to the "inverse url" by swapping the cities around

if ($city1 != in_array($allowed_left) ){
   if ($city2 != in_array($allowed_left)){
      header("Location: http://sometravelwebsite.com/destinations/compare/not_allowed.php"); 
   }
   else {
      header("HTTP/1.1 301 Moved Permanently"); 
      header("Location: http://sometravelwebsite.com/destinations/compare/".$city2."-vs-".$city1);
   }
}

This obviously assumes that one of the cities will be allowed on the left, otherwise you might end up in an infinite loop if neither of them are on the allowed list..

Nicolas Marengo
  • 309
  • 1
  • 5
  • Yeah, unfortunately, this solution would run into infinite loops, because if I had set New York as $allowed_left, it is possible (and very likely) that at some point someone will want to compare Milan and London, which creates the issue you referenced above. Thank you though! – will74 Apr 15 '14 at 13:51
  • Well the redirect will only happen if the city is NOT allowed on the left. Comparing Milan and London will work, since milan IS in the allowed array without swapping them around. if that makes sense – Nicolas Marengo Apr 24 '14 at 09:40
  • In the above example, it will run into an Infinite loop if you for example want to compare Madrid and Paris. As neither of them are in the allowed left list. You could put another check inside that if statement that if city2 is also not on the allowed list then go to a 404 page or a "Your combination is not allowed" page.. will edit solution – Nicolas Marengo Apr 24 '14 at 09:43
0

The dynamic PHP handles both but if they come from a search engine you want a redirect? Stick to a simple naming convention or always order them in alphabetically order or something that way you won't have separate variations. Im sure Google won't mind if its 'A Vs B' or 'B Vs A' im sure it would return both equally if useful to the search.

You can can put the better performing object on the left. Make the URL always alphabetically, put the 2 cities (or more) into array and sort alphabetically. As for displaying on the page, query your database and display them left to right(Sort on your MySQL Statement) based on the calculated 'score' of the one you think will convert higher.

Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58