Clean up URLs with PHP


Clean up URLs with PHP

我正在对一个网站进行编码,URL中的关键字如下:

?s=2010%20Federal%20Spending&id=115

标题为"2010年联邦支出"的部分不用于导航;我的网站导航完全忽略了它。我的网站只是关注id,而不是s。同样,这个标题只是出于SEO的原因。

有没有PHP函数可以清理URL的这一部分?例如,将"%20"替换为"-"或类似的内容?

您需要查看.htaccess 中的mod_rewrite

在.htaccess中添加重写规则很简单。首先,通过将此行添加到.htaccess:来激活mod_rewrite

RewriteEngine on
RewriteBase /

然后添加重定向页面的规则:

RewriteRule ^([0-9]+)/([^/]+)$ /yourpage'.php?id=$1&s=$2

这将允许你构建这样的网址:

yoursite.com/115/2010-federal-spending

然后,在您的页面上。hp:

echo $_GET['id']; // will equal 115 from the above example
echo $_GET['s']; // will equal 2010-federal-spending from the above example

如果您想解码URL,请使用urldecode($your_string(。由于空格不是一个有效的URL字符,所以在将其用作地址之前,您可能应该尝试替换标题中的空格。

$mytitle = "2010 Federal Spending";
$fixedtitle = str_replace(" ", "_", $mytitle);
echo $fixedtitle;

您还可以删除其他可能导致一些问题的CHAR,如"&">

$mytitle = "2010 Federal Spending";
$invchars = array(" ","@",":","/","&");
$fixedtitle = str_replace($invchars, "_", $mytitle);
echo $fixedtitle;
?s=2010%20Federal%20Spending&id=115

这是一个编码的url,空的"已编码到"%20"中,您不想替换它,而是先解码

$url=urldecode('?s=2010%20Federal%20Spending&id=115'(

现在用你喜欢的任何东西替换空字符串,最后做

$newUrl = str_replace(' ' ,'-',$url); 
echo urlencode($newUrl);

您也可以使用此处描述的函数(法语(:

    /**
     * Convert into filename by removing all accents and special characters. Useful for URL Rewriting.
     * @param $text
     * @return string
     */
    public function ConvertIntoFilename($text)
    {
        // Remove all accents.
        $convertedCharacters = array(
            'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A',
            'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a',
            'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O',
            'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o',
            'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E',
            'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
            'Ç' => 'C', 'ç' => 'c',
            'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
            'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
            'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U',
            'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
            'ÿ' => 'y',
            'Ñ' => 'N', 'ñ' => 'n'
        );
        $text = strtr($text, $convertedCharacters);
        // Put the text in lowercase.
        $text = mb_strtolower($text, 'utf-8');
        // Remove all special characters.
        $text = preg_replace('#[^a-z0-9-]#', '-', $text);
        // Remove two consecutive dashes (that's not very pretty).
        $text = preg_replace('/--/U', '-', $text);
        // Remove words containing less than 2 characters (non significant for the meaning)
        $return = array();
        $text = explode('-', $text);
        foreach($text as $word)
        {
            if(mb_strlen($word, 'utf-8') <= 2)   continue;
            $return[] = $word;
        }
        return implode('-', $return);
    }

然而,它仍然需要你修改你的.htaccess,就像AlienWebGuy提到的那样。:(