0

Let me first give you a little background to explain what I'm trying to do. My websites use URL's that look like this: MySite/World/Isthmus_of_Panama

I'm working on a major upgrade (and may eventually upgrade further by switching to a CMS, like Drupal or WordPress), and it sounds like the general consensus is that URL's with hyphens are better than underscores. So I'm changing my URL's to MySite/World/Isthmus-of-Panama. In the meantime, I'm also trying to figure out if I should change my URL's to all lower case, and what about special symbols like accents or parentheses?

And what if someone typed in a URL that looks like MySite/World/Isthmus of Panama ? Wikipedia has a script that automatically converts the spaces to underscores. It will also default to the correct URL if you use the wrong case.

Of course, if I change my URL's, I'll also have to forward visitors from my old URL's. It's getting very confusing.

Then I realized that I could cover all of the bases with a script that accepts any URL that matches the characters in my database, 1) regardless of case, 2) and regardless of whether multiple words are separated by hyphens, underscores, spaces or %20. So imagine the following URL's:

MySite/World/Isthmus-of-Panama
MySite/World/Isthmus of Panama
MySite/World/Isthums%20of%20Panama
MySite/World/isthumus_of_panama
MySite/World/Isthmus-of_PANAMA

Where the database value is Isthmus-of-Panama.

Below is one of my queries, where $MyURL = the database value URL (e.g. Isthmus-of-Panama). Can anyone tell me how to modify it so that all of the above URL's will be accepted, with the page then defaulting to the database value?

Wikipedia has a similar feature. If you go to their article about Crazy Horse, then replace the URL Crazy_Horse with crazy_horse or Crazy Horse, it will default to Crazy_Horse. Thanks.


$sql= "SELECT COUNT(URL) AS num FROM gs_reference
WHERE URL = :MyURL";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();


switch($Total['num'])
{
 case 1:
 // DISPLAY A PAGE
 break;
 case 0:
 // 404 NOT FOUND ERROR
 break;
 default:
 // DUPLICATE RESULTS
 break;
}
David Blomstrom
  • 1,379
  • 3
  • 16
  • 29

1 Answers1

1

I would convert input, example Isthums%20of%20Panama, to the database value in php. If the converted value is equal to the input one then don't do a 301 redirect to the url with the converted text else do one

EDIT

I would create in database a column slug (generally called like this) which contain the text normalized (ascii character and -) and create an unique index on it You could use this function to generate the slug in php: PHP function to make slug (URL string)

Community
  • 1
  • 1
Shire
  • 408
  • 2
  • 9
  • I think I understand what you're saying; if the characters and spaces/hyphens/etc. match the database value exactly, then simply display the page. If the characters match - except for differences in case or hyphens vs underscores vs spaces - then redirect visitors to the correct URL. But can someone suggest a script that would accomplish this - a script that says "All the characters match the database value, and I don't care whether there's a -, _ or (space) between multiple words"? – David Blomstrom Mar 09 '14 at 05:26