Desarrollo web. Redes sociales, Herramientas Web y más

URLs SEO con PHP y jQuery

27/01/2010 |    |  share  |   Un comentario

Las URLs amigables se caracterizan por ser descriptivas y fáciles de escribir, también por tener un formato de caracteres alfanuméricos en minúsculas, separando las palabras por guiones “-”.

En este tutorial aprenderemos como realizar un formulario que nos devuelva la URL SEO en base al titulo.

 

 

1. Estructura HTML

Dentro del head incluiremos la librería jQuery desde Google y crearemos una función que nos retornara una petición Ajax.

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
    var send = function(){
        $.ajax({ url: "index.php", data: {title: $("#title").val() }, success: function(d){
            $('#urlseo').html("<strong>permalink: </strong>"+d);
          }});
    }
    </script>
</head>

Ahora crearemos el formulario donde ingresaremos un texto y nos devolverá la misma en URL SEO.

<body>
    <form action="" style="width:500px;">
        <input type="text" id="title" name="title"/>
        <input type="button" id="ssend" name="ssend" value="Generar" onclick="send()" />
        <div id="urlseo"></div>
    </form>
</body>

2. Creando la función PHP

Esta función PHP nos filtrara los caracteres alfanuméricos y guiones eliminando el resto, y de esta manera generaremos la URL.
Este script lo pegaremos en el mismo archivo definido en la función javascript, para que retorne la URL.

<?php
function getUrlAmigable($s){

    $s = strtolower($s);
    $s = ereg_replace("[áàâãäª@]","a",$s);
    $s = ereg_replace("[éèêë]","e",$s);
    $s = ereg_replace("[íìîï]","i",$s);
    $s = ereg_replace("[óòôõºö]","o",$s);
    $s = ereg_replace("[úùûü]","u",$s);
    $s = ereg_replace("[ç]","c",$s);
    $s = ereg_replace("[ñ]","n",$s);
    $s = preg_replace( "/[^a-zA-Z0-9\-]/", "-", $s );
    $s = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $s);

    return trim($s, '-');
}

if(isset($_GET['title'])){
    $url = getUrlAmigable(utf8_decode($_GET['title']));
    echo $url;exit;
}
?>

Ejemplo

En este ejemplo podemos testear la función PHP generando URLs SEO, veremos que elimina las tildes y caracteres raros.

 

Comparte este Post

Tags

, ,

Post Relacionados

Deja tu Comentario

1 Comentarios para este Post

  1. uberVU - social comments Dice:

    Social comments and analytics for this post…

    This post was mentioned on Facebook by Cesar Mancilla: URLs SEO con PHP y jQuery | http://tinyurl.com/y8wkdtq…