htaccess重定向和重写不重定向到URL

时间:2014-02-26 08:50:37

标签: php .htaccess mod-rewrite redirect

我是htaccess重定向和重写的新手。我已经转换了以下网址

example.in/celeb_gallery.php?celeb_id=1

作为

example.in/celebrity/1 通过以下htaccess代码

Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^celeb_id=(.*)$
RewriteRule ^celeb_gallery\.php$ /celebrity/%1? [R=301]

再次获取celeb_id的值我在htaccess文件中添加了followig代码

RewriteRule ^celebrity/([^-]+)$ /celeb_gallery.php?celeb_id=$1  [L]

我上传了htaccess文件。当网址example.in/celeb_gallery.php?celeb_id=1被移动时,它会在地址栏中更改为example.in/celebrity/1

但网页在Chrome中显示错误This Webpage has a direct loop,在Firefox中显示The page isn't redirecting properly。为什么它不工作......?

示例工作sample.php文件仅包含以下代码:它在没有任何css的情况下工作。

<?php
echo "This is an sample page<br>";
echo "Celeb id:".$_REQUEST["celeb_id"]; 
echo "<br>";
?>

celeb_id正在传递celeb_gallery.php文件,但它没有打开我认为的任何样式表。代码在这里:

<?php include('cpanel/common/dbconnect.php');?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<?php
$ncel = "SELECT * FROM celebrity WHERE cel_id = ".$_REQUEST['celeb_id']."";
$ncel_e = mysql_query($ncel);
$ncel_f = mysql_fetch_array($ncel_e)
?>
<title><?=$ncel_f['name']?> | Site Title</title>
<?php include('common/css_js.php'); ?>
</head>

样式表在<?php include('common/css_js.php'); ?>中调用,而我认为它不起作用。

1 个答案:

答案 0 :(得分:0)

如果您想将example.in/celeb_gallery.php?celeb_id=1的来电重定向到example.in/celebrity/1,您只需要:

Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^celeb_id=([\d]+)$
RewriteRule ^celeb_gallery\.php$ /celebrity/%1 [L, R=301]

但是,如果您希望调用像example.in/celebrity/1这样的网址来处理调用example.in/celeb_gallery.php?celeb_id=1

所以你需要:

Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteRule ^celebrity/([\d]+)$ /celeb_gallery.php?celeb_id=$1 [L]

如果规则匹配,[L]标志将使htaccess停止处理其他规则