【问题标题】:Dynamic URL rewriting动态 URL 重写
【发布时间】:2023-03-06 05:02:01
【问题描述】:

我正在开发一个电子商务网站,我正在努力改进其 URL。我启用了 Apache 配置,然后创建了一个 .htaccess 文件,但我的 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]`

【问题讨论】:

  • \$ 是错误的,您正在转义 $ 符号而不是 /?$
  • .htaccess 并不是最好的方法。考虑使用路由器或自行编程。
  • 尝试了您要求的更改,但网址仍然趋于相同。感谢@Rahil 的建议,任何其他建议也将是 hlpful
  • 你能告诉我或详细说明一下使用路由器@Xatenev
  • 我很困惑我使用的 php echo 函数是否正确?

标签: php apache .htaccess mod-rewrite


【解决方案1】:

重写引擎开启 重写基础/

#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]

【讨论】:

  • 我也需要更改php文件中的href标签吗? @Ajay
【解决方案2】:

您需要删除此条件的最后一个实例:

RewriteCond %{REQUEST_FILENAME}\.php -f

此条件会阻止您进入重写规则,因为如果不重写 URL,则无法满足条件。 您还需要更改最后的重写规则如下:

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

【讨论】:

  • 你是对的!这是因为 RewriteCond...我编辑了答案以解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2011-06-02
  • 2021-11-06
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多