.htaccess重写规则不起作用(启用重写模块)

时间:2016-08-25 11:49:00

标签: php apache .htaccess mod-rewrite

我想在$_GET['pid']页面上获取products.php的价值,看起来它对我不起作用,我们将不胜感激。

.htaccess重写规则如下

RewriteEngine On
RewriteRule ^products/([0-9]+)$ /products.php?pid=$1 [NC,L]

的index.php

<?php 
$i=1;
while($i <= 10){
    echo '<a href="products.php?pid='.$i.'">Products '.$i.'</a><br />';
    $i++;
}
?>

products.php

<?php 
if(!isset($_GET['pid'])){
    header('location : index.php');
}
else if(isset($_GET['pid'])){
    echo 'Product id - '.$_GET['pid'];
}
?>

2 个答案:

答案 0 :(得分:0)

您的意思是链接没有正确显示? 你的php文件必须输出链接,mod_rewrite稍后会“翻译”。见下面的例子:

<?php 
$i=1;
while($i <= 10){
echo '<a href="products/'.$i.'">Products '.$i.'</a><br />';
$i++;
}
?>

答案 1 :(得分:0)

我们使用Htaccess将“丑陋”的URL重写为更短更漂亮。在您的代码中,您可以更改htaccess。

RewriteEngine On
RewriteRule ^(.*)$ products.php?pid=$1 [QSA,L]

然后更改您的网址。

  <?php 
       $i=1;
       while($i <= 10){
       echo '<a href="products.php/'.$i.'">Products '.$i.'</a><br />';
       $i++;
      }
   ?>