dqlxtv1452 2015-05-03 08:31
浏览 39

将URL更改为php中的另一种格式

my index.php is :

<?php
    ob_start();
    include 'dataload.php';
    /********/
    ini_set("display_errors","0");
    ini_set("register_globals","0");    

    /********/
    $pageid = isset($_GET['pageid'])?intval($_GET['pageid']):'1';

    $servername = strtolower($_SERVER['SERVER_NAME']);
    $servername = (substr($servername,0,4) == 'www.')?substr($servername,4):$servername;

/*
    //visit
    $res=mysql_query("update `amar` set `count`=count+1 where `date`='".date("Y-m-d")."' limit 1;");
    if(mysql_affected_rows()!=1){
        mysql_query("INSERT INTO `amar` (`id`, `date`, `count`, `click`) VALUES (NULL, '".date("Y-m-d")."', '1', '0');");
    }
*/          
    #------
    if(preg_match('#([a-z0-9-]+?)\.mysite\.ir#i', $servername, $blogname)){
        if(isset($_GET['id']) ){
            $res=mysql_query("select id,username from `feeds` where `id`=(select fid from `news_tmp` where `id`=$_GET[id]) limit 1");
            if(mysql_num_rows($res)>0){
                list($id,$username)=mysql_fetch_array($res);
                if($blogname[1]!=$username){
                    Header( "HTTP/1.1 301 Moved Permanently" );
                    Header( "Location: http://$username.mysite.ir/page-$_GET[id].html" ); 
                    exit;               
                }
            }
        }
        $username=$blogname[1];
        $res=mysql_query("select id,title from `feeds` where `username`='{$blogname[1]}'");
        if(mysql_num_rows($res)>0){
            list($id,$title)=mysql_fetch_array($res);
            $res=mysql_query("select `title`,`text`,`date`,`keywords` from `news_tmp` where `fid`=$id ".(isset($_GET['id'])?"and `id`=$_GET[id] ":'')." /*order by `date` DESC*/");
            while($row=mysql_fetch_assoc($res)){
            if(isset($_GET['id']))$title=$row['title'];
            ?>
                              <div class="post-title">
                                <div class="post-title-space">
                                  <h1><span lang="fa"><?=$row['title'];?></span></h1>
                                </div>
                                <div id="post-content">
                                  <div class="post-content-text">
                                      <div class="style2">
                              <span lang="fa"><?=$row['text'];?></span>
                        </div>
                                    <div class="clear"></div>
                                    <div class="clear"></div>
                                  </div>
                                </div>
                                <div id="post-footer"></div>
                              </div>
            <?
            }
        }else{
            echo 'user not found in feed.';     
        }
    }
    else
    {
        if(isset($_GET['id']) ){
            $res=mysql_query("select id,username from `feeds` where `id`=(select fid from `news_tmp` where `id`=$_GET[id]) limit 1");
            if(mysql_num_rows($res)>0){
                list($id,$username)=mysql_fetch_array($res);
                Header( "HTTP/1.1 301 Moved Permanently" );
                Header( "Location: http://$username.royalnews.ir/page-$_GET[id].html" ); 
                exit;
            }
        }
        $res=mysql_query("select `title`,`id` from `news_tmp` order by `date` DESC limit 200");
        if(mysql_num_rows($res)>0){
            echo '<ul>';
            while($row=mysql_fetch_assoc($res)){
                echo "<li><a href=\"page-$row[id].html\" target=\"_blank\">$row[title]</a></li>
";
            }
            echo '</ul>';               
        }
        echo 'user not found.';
    }
    $out_html=ob_get_clean();   
    include 'tmp.php';
?>

This code open links in this format : username.mysite.com now i want open my links with mysite.com/username username gives from feed table column username . please help me to solve this problem .

my httacces file is this :

RewriteEngine On
#RewriteBase /
RewriteRule page-([0-9]*).html ./index.php?id=$1 [L]
RewriteRule ^list/(.*)-([0-9]*)\.html$ ./search.php?key=$1&start=$2 [L]
RewriteRule ^list/(.*)\.html$ ./search.php?key=$1 [L]
Rewriterule ^rss.xml ./rss/rss.php
### EXPIRES
# month A2592000, week A604800, day A86400, hour A3600,15 min A900
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A900
php_value max_execution_time 120
RewriteEngine On
RewriteRule .* - [E=noabort:1, E=noconntimeout:1]

</div>
  • 写回答

1条回答 默认 最新

  • dongsou4301 2015-05-03 08:41
    关注

    You can use preg_replace() to transform the URL.

    If dots are allowed within usernames, then you need to explicitly specify the site name in the regular expression:

    $newUrl = preg_replace('/^([A-Za-z0-9_\.-]+)\\.example\\.com$/', 'example.com/\\1', $url)
    

    or, to redirect from .htaccess:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^([A-Za-z0-9_\.-]+)\.example\.com$
    RewriteRule (.*) http://example.com/%1/$1 [L,QSA]
    

    If you don't allow dots within the username, then you can use a more simplified and flexible regular expression:

    $newUrl = preg_replace('/^([A-Za-z0-9_-]+)\.(.*)$/', '\\2/\\1', $url)
    

    or, if you want to redirect from .htaccess:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^([A-Za-z0-9_-]+)\.(.*)$
    RewriteRule (.*) http://%2/%1/$1 [L,QSA]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?