如何通过htaccess缩短网址?

时间:2013-01-26 12:45:29

标签: php .htaccess str-replace

我有这个.htaccess行:

RewriteRule ^forum_(.*).html forum.php?frm=$1 [L]

我有这个php行:

$url = str_ireplace('forum.php?frm=', 'forum_.html', $url);

但网址变成这样:

forum_.html1

我希望网址变得像:

forum_1.html

怎么做??

完整的.htaccess文件:

    RewriteEngine On
# begin index Page Rewriting
RewriteRule ^index index.php [L]
RewriteRule ^index.html index.php [L]
RewriteRule ^index.htm index.php [L]
RewriteRule ^forum_(.*)\.html forum.php?frm=$1 [L]
RewriteRule ^topics_(.*) thread.php?tpc=$1 [L]
RewriteRule ^tpcs_auth_(.*) tpcs.php?auth=$1 [L]
RewriteRule ^tpcs_rauth_(.*) tpcs.php?rauth=$1 [L]
RewriteRule ^users_(.*) user.php?id=$1 [L]
RewriteRule ^chat.html chat.php [L]
RewriteRule ^signup.html signup.php [L]
RewriteRule ^users.html users.php [L]
RewriteRule ^cp.html Upanel.php [L]
RewriteRule ^messages.html pm.php [L]
RewriteRule ^style=(.*) changetemplate.php?action=changestl&styleid=$1 [L]
RewriteRule ^lostpw.html lostpw.php [L]

# end

完整的seo.php文件:

    $seotp = $config->Cquery("site_seo");
if($seotp == 0)ob_start();

function change_links(){
global $seotp;
if($seotp == 0){
$contents = ob_get_contents();
$contents = rewrite($contents);
ob_end_clean();
echo $contents;
}
}

function rewrite($url){
global $seotp;
if($seotp == 0){
$url = str_ireplace('href="index.php', 'href="index.html', $url);
$url = str_ireplace('href="./index.php', 'href="./index.html', $url);
$url = str_ireplace('href="./forum.php?frm=', 'href="./forum_.html', $url);
$url = str_ireplace('forum.php?frm=', 'forum_.html', $url);
$url = str_ireplace('href="./thread.php?tpc=', 'href="./topics_', $url);
$url = str_ireplace('thread.php?tpc=', 'topics_', $url);
$url = str_ireplace('href="./user.php?id=', 'href="./users_', $url);
$url = str_ireplace('user.php?id=', 'users_', $url);
$url = str_ireplace('tpcs.php?auth=', 'tpcs_auth_', $url);
$url = str_ireplace('tpcs.php?rauth=', 'tpcs_rauth_', $url);
$url = str_ireplace('href="./tpcs.php?auth=', 'href="./tpcs_auth_', $url);
$url = str_ireplace('href="./tpcs.php?rauth=', 'href="./tpcs_rauth_', $url);
$url = str_ireplace('href="chat.php', 'href="chat.html', $url);
$url = str_ireplace('href="users.php', 'href="users.html', $url);
$url = str_ireplace('href="./users.php', 'href="./users.html', $url);
$url = str_ireplace('href="singup.php', 'href=".singup.html', $url);
$url = str_ireplace('href="./singup.php', 'href="./singup.html', $url);
$url = str_ireplace('href="./Upanel.php', 'href="./cp.html', $url);
$url = str_ireplace('href="Upanel.php', 'href="cp.html', $url);
$url = str_ireplace('href="./pm.php', 'href="./messages.html', $url);
$url = str_ireplace('href="pm.php', 'href="messages.html', $url);
$url = str_ireplace('href="./lostpw.php', 'href="./lostpw.html', $url);
$url = str_ireplace('href="lostpw.php', 'href="lostpw.html', $url);
$url = str_ireplace('href="./changetemplate.php?action=changestl&styleid=', 'href="./style=', $url);
$url = str_ireplace('href="changetemplate.php?action=changestl&styleid=', 'href="style=', $url);
return $url;
}
}

调用函数时:change_links();

1 个答案:

答案 0 :(得分:1)

你应该使用preg_replace:

$url = preg_replace('/forum.php\?frm=([0-9]+)/', 'forum_$1.html', $url);

使用正则表达式可以解决您的问题。