如何通过htaccess的缩短网址是什么? [英] how to shorten url by htaccess?

查看:140
本文介绍了如何通过htaccess的缩短网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的.htaccess行:

I have this .htaccess line:

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

和我有这个PHP行:

and I have this php line :

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

但是URL变成了这个样子:

but the url become like this:

forum_.html1

和我想的网址变成这样的:

and I want the url to become like:

forum_1.html

如何做到这一点?

how to do that??

完整的.htaccess文件:

the full .htaccess file :

    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文件:

the full seo.php file:

    $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();

推荐答案

您应该使用preg_replace:

You should use preg_replace:

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

在使用正则表达式将解决您的问题。

The use of a Regexp will solve your problem.

这篇关于如何通过htaccess的缩短网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆