我的index.php是:
<?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>\n";
}
echo '</ul>';
}
echo 'user not found.';
}
$out_html=ob_get_clean();
include 'tmp.php';
?>
此代码以以下格式打开链接: username.mysite.com 现在我想用mysite.com/username打开我的链接 用户名来自Feed表列用户名。 请帮我解决这个问题。
我的httacces文件是这样的:
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]
答案 0 :(得分:0)
您可以使用preg_replace()转换网址。
如果用户名中允许使用点,则需要在正则表达式中明确指定站点名称:
$newUrl = preg_replace('/^([A-Za-z0-9_\.-]+)\\.example\\.com$/', 'example.com/\\1', $url)
或,从.htaccess
重定向:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9_\.-]+)\.example\.com$
RewriteRule (.*) http://example.com/%1/$1 [L,QSA]
如果您不允许用户名中的点,那么您可以使用更简化和灵活的正则表达式:
$newUrl = preg_replace('/^([A-Za-z0-9_-]+)\.(.*)$/', '\\2/\\1', $url)
或者,如果您想从.htaccess
重定向:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9_-]+)\.(.*)$
RewriteRule (.*) http://%2/%1/$1 [L,QSA]