2014-06-24 113 views
0

我正在一个电子商务网站上工作,我正在努力使其URL更好。我启用了Apache配置,然后创建了一个.htaccess文件,但我的URL仍然相同。我无法弄清楚我错在哪里。动态URL重写

如果我的网址是这样的:http://www.public_html/products.php?scatid=72

,我希望它看起来像这样:http://www.public_html.com/products/scatid/72

我有我的PHP文件:

if($cntrec_prod > 0){ 
    $cnt = 0; 
    mysql_data_seek($srsprod_mst,0);   
    while($srowsprod_mst=mysql_fetch_assoc($srsprod_mst)){ 
     $db_catone_id = $srowsprod_mst['prodcatm_id']; 
     $db_catone_name = $srowsprod_mst['prodcatm_name']; 
     $db_cattwo_id = $srowsprod_mst['prodscatm_id']; 
     $db_cattwo_name = $srowsprod_mst['prodscatm_name'];  
     $db_dys_lft  = $srowsprod_mst['dyslft'];    
     $smlimg_lst  = $srowsprod_mst['prodimgd_simg']; 
     $avl_prd  = $srowsprod_mst['avlprd'];  
     //$lnkname = "products.php?scatid=$db_cattwo_id";   
     $ary_imgnm = explode('--',$smlimg_lst); 
     $img_cnts = ''; 
     $img_frstcnts =''; 
     $imginc = 0; 
     $scatid_lst .= "--".$db_cattwo_id.'-'.$db_dys_lft; 
     for($inc=0;$inc<count($ary_imgnm);$inc++){      
      $scat_imgnm  = $ary_imgnm[$inc]; 
      $scat_imgpth  = $u_sml_upldpth.$scat_imgnm;     
      if(($scat_imgnm != '') && file_exists($scat_imgpth)){ 
       $imginC++; 
       if($inc == 0){ 
        $img_frstcnts = "<a href='products.php?scatid=$db_cattwo_id' class='catImg'><img src='$scat_imgpth' width='300' height='350' border='0' alt=''></a>"; 
       } 
       $img_cnts = "<a href='products.php?scatid=$db_cattwo_id'><img src='$scat_imgpth' width='300' height='350' /></a>";      
      } 
      else{ 
       $img_frstcnts = "<a href='products.php?scatid=$db_cattwo_id' class='catImg'><img src='images/noimage.jpg' width='300' height='350' border='0' alt=''></a>"; 
      } 
     }      
     $dsp_name = $db_cattwo_name;    
?> 

我的.htaccess文件包含以下代码:

RewriteEngine on 
RewriteBase /public_html/ 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 
RewriteRule products/scatid/(.*)/ products.php?scatid=$1 [L,QSA] 
RewriteRule products/scatid/(.*) products.php?scatid=$1 [L,QSA]` 
+0

'\ $'是错误的,你都逃脱'$'符号,而不是做'/? $' –

+0

。一切的htaccess不是最好的方法。考虑使用**路由器**或自行编程。 – Xatenev

+0

试过这个改变,你仍然要求URL一致。感谢您的建议@Rahil ny其他建议也将是hlpful – Priyanka

回答

0

您需要移除这COND的最后一个实例:

RewriteCond %{REQUEST_FILENAME}\.php -f 

这COND阻止你获得的重写规则,因为如果网址不被改写的COND无法满足。 您还需要改变最后重写规则如下:

RewriteRule ^sub-products/([0-9]+)$ sub-products?catid=$1 [NC,L] 
+0

不,它不工作。 @ clami219 – Priyanka

+0

你是对的!这是因为RewriteCond ...我编辑了解决问题的答案 – clami219

0

RewriteEngine叙述在 RewriteBase/

#add these 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) /$1/ [R,L] 

RewriteCond %{HTTP_HOST} !^example.nl$ [NC] 
RewriteRule ^(.*)$ http://example.nl/$1 [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 
+0

我是否需要更改php文件中的href标签? @Ajay – Priyanka

+0

不,你不需要。 –