简体   繁体   中英

how to shorten url by htaccess?

I have this .htaccess line:

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

and I have this php line :

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

but the url become like this:

forum_.html1

and I want the url to become like:

forum_1.html

how to do that??

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

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;
}
}

when calling function : change_links();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM