无法使用.htaccess重定向到quotes.php

时间:2015-07-29 06:38:13

标签: php .htaccess redirect href

我有三页index.php,author.php和quotes.php 在index.php中我给出了author.php的参考,在author.php中我给出了quotes.php的参考。 我能够重定向到author.php,但无法重定向到quotes.php这里是我所有三个文件的代码。 即使引用quotes.php.both的时间文件重定向到author.php。 帮助将不胜感激。

 index.php

 <!DOCTYPE html>
 <html>
 <title>Quotes</title>
 <head>
 <style>
 .dec {
 text-align:center;
 background-color: #000;
 background-image: linear-gradient(to bottom,#383838,#080808);
 border: 1px solid #000;
 min-height: 30px;
 padding-bottom: 3px;
 padding-top: 12px;
 }
 .dec a{
 color:white;
 text-decoration:none;
 }
  a:hover{
  color:grey;
  text-decoration:underline;
 }
table a{color:inherit;text-decoration:none;}
table a:hover{text-decoration:underline;}
 </style>
</head>
<body>
<div class ="dec">
<?php
 $chars = range('a', 'z');
 foreach($chars as $eachChar){
 echo '<a      href="http://localhost/quotes/authors/'.$eachChar.'.html">'.strtoupper($eachChar ).'</a>&nbsp;&nbsp;';
 }
 ?>
 </div>
 <?php include_once('dbconnect.php'); ?>
 <?php 

  $GetChR=isset($_REQUEST['authchar'])?$_REQUEST['authchar']:'a';
  $query= "SELECT * FROM `table2` WHERE `col2` like      '".$GetChR."%'";//.$GetChR."%'";
 $dd = mysqli_query($con,$query);


  if($dd === FALSE) { 
  die(mysqli_error($con)); // TODO: better error handling
   }
  $output='';
  $count=1;
  $NothingFound=true;
  while($row = mysqli_fetch_array($dd))
  {
  $NothingFound=false;
  $output .='<tr><td style="width:3%;">'.$count.'.</td><td style="width:97%;">';
   $output .='<a  href="http://localhost/quotes/authors/'.$GetChR.'/'.$row['friendly_url'].'.html" >';
  $output .=$row['col2'].'</a></td></tr>';//here I  have given link so that author.php should redirect and it is working fine than what  is the  mistake in below link why it is not redirecting to quotes.php
  $count++;
    } 
    if($NothingFound){
    $output .='<tr><td>Nothing Found.</td></tr>';
     }
     ?>
      <table border="5" cellpadding="5" cellspacing="0" style="border-      collapse: collapse" bordercolor="#808080" width="100&#37;" id="AutoNumber2"   bgcolor="white">
       <tr><td width=100 colspan="2"><strong>Author Name</strong></td></tr>
       <?php echo  $output; ?>
       </table>
       </body>
       </html>

author.php

     <!DOCTYPE html>
     <html>
     <title>Quotes</title>
     <head>
     <style>
     .dec {
      text-align:center;
      background-color: #000;
      background-image: linear-gradient(to bottom,#383838,#080808);
      border: 1px solid #000;
      min-height: 30px;
      padding-bottom: 3px;
      padding-top: 12px;
       }
      .dec a{
       color:white;
       text-decoration:none;
        }
        a:hover{
        color:grey;
        text-decoration:underline;
        }
        table a{color:inherit;text-decoration:none;}
        table a:hover{text-decoration:underline;}
        </style>
        </head>
        <body>
        <div class ="dec">
        <?php
        $chars = range('a', 'z');
        foreach($chars as $eachChar){
        echo '<a   href="http://localhost/quotes/authors/'.$eachChar.'.html">'.strtoupper($eachChar              ).'</a>&nbsp;&nbsp;';
          }
          ?>
          </div>
          <?php include_once('dbconnect.php'); ?>
          <?php 
            $authorname=isset($_REQUEST['authorname'])?      $_REQUEST['authorname']:'';
             $query= "SELECT table2.col2 AS a,table1.col2 AS b, table1.col1    AS c, table1.q_url AS d FROM table2, table1 WHERE table2.col1 = table1.col4 AND    table2.friendly_url= '".$authorname."'";
             $dd = mysqli_query($con,$query);
             if($dd === FALSE) { 
             die(mysqli_error($con)); // TODO: better error handling
              }
              $output='';
              $Authorname='';
              $count=1;
              while($row = mysqli_fetch_array($dd))
              {
               $Authorname =$row['a'];
               $output .='<tr><td style="width:3%;">'.$count.'.</td><td   style="width:97%;">';
               $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
               $url=explode('/',$url);
               $output .='<a     href="http://localhost/quotes/authors/'.$url[5].'/'.$row['d'].'.html">';
               $output .=$row['b'].'</a></td></tr>';//here I  have given so that qoutes.php should redirect but it is not. is there any  mistake
               $count++;
               }
               ?>
               <table border="5" cellpadding="5" cellspacing="0"   style="border-collapse: collapse" bordercolor="#808080" width="100&#37;"  id="AutoNumber2" bgcolor="white">
              <tr><td width=100 colspan="2"><strong><?php echo $Authorname;   ?> Quotes</strong></td></tr>
              <?php echo  $output;?>
              </table>
              </body>
              </html>

quotes.php

       <!DOCTYPE html>
       <html>
       <title>Quotes</title>
       <head>
       <style>
       .dec {
        text-align:center;
        background-color: #000;
        background-image: linear-gradient(to bottom,#383838,#080808);
        border: 1px solid #000;
        min-height: 30px;
        padding-bottom: 3px;
        padding-top: 12px;
         }
        .dec a{
         color:white;
         text-decoration:none;
          }
         a:hover{
         color:grey;
         text-decoration:underline;
         }
         table a{color:inherit;text-decoration:none;}
         table a:hover{text-decoration:underline;}
         </style>
         </head>
         <body>
         <div class ="dec">
         <?php
         $chars = range('a', 'z');
         foreach($chars as $eachChar){
         echo '<a   href="http://localhost/quotes/'.$eachChar.'.html">'.strtoupper($eachChar).'</a>&nbsp;&nbsp;';
         }
         ?>
         </div>
         <?php include_once('dbconnect.php'); ?>
         <?php 
         $qname=isset($_REQUEST['qname'])?$_REQUEST['qname']:'';
         $query= "SELECT col2 FROM  table1 WHERE q_url ='".$qname."'";
         $dd = mysqli_query($con,$query);
         if($dd === FALSE) { 
         die(mysqli_error($con)); // TODO: better error handling
         }
         $output='';
         $Authorname='';
         $count=1;
         while($row = mysqli_fetch_array($dd))
         { 
          $output .='<tr><td style="width:3%;">'.$count.'.</td><td  style="width:97%;"><p>'.$row['col2'].'</p></td></tr>';
          $count++;
          }
          ?>
          <table border="5" cellpadding="5" cellspacing="0" style="border-  collapse: collapse" bordercolor="#808080" width="100&#37;" id="AutoNumber3"   bgcolor="white">
          <tr><td width=100 colspan="2"><strong><?php echo $Authorname; ?>   Quotes</strong></td></tr>
          <?php echo  $output; ?>
          </table>
          </body>
          </html>

的.htaccess

      AddDefaultCharset utf-8
      Options +FollowSymlinks -Indexes
      RewriteEngine on 
      RewriteRule ^authors/([^/]*)\.html$ /quotes/index.php?authchar=$1 [L]
      RewriteRule ^authors/([^/]*)/([^/]*)\.html$ /quotes/author.php?  authchar=$1&authorname=$2 [L]
      RewriteRule ^authors/([^/]*)/([^/]*)/([^/]*)\.html$ /quotes/quotes.php?  authchar=$1&authorname=$2&qname=$3 [L]

1 个答案:

答案 0 :(得分:3)

除了拼错qoutes.php之外,你的主要问题是你有两个相同的正则表达式,映射到2个不同的目标。所以会发生什么是第一条规则总是得到匹配,第二条规则永远不会做任何事情。你需要做一些事情来使正则表达式模式不同,就像你看起来像你可能尝试这样做:

 Options +FollowSymLinks
 RewriteEngine On
 RewriteRule ^authors/([^/]*)\.html$ /quotes/index.php?authchar=$1 [L]
 RewriteRule ^authors/([^/]*)/([^/]*)\.html$ /quotes/author.php?authchar=$1&authorname=$2 [L]
 RewriteRule ^authors/([^/]*)/([^/]*)/([^/]*)\.html$ /quotes/quotes.php?authchar=$1&authorname=$2&qname=$3 [L]