【问题标题】:convert multiple Query String to seo friendly url in php在php中将多个查询字符串转换为seo友好的url
【发布时间】:2025-12-16 00:10:01
【问题描述】:

我有 url Like http://somesite.com/packagemenu.php?conname=domestic-tours-packages&consubname=kerala-tours-packages

现在,我想通过 php 代码或 htaccess rewriterule 使其对 seo 友好

我的输出应该是这样的

http://somesite.com/domestic-tours-packages/kerala-tours-packages

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:

    只需像这样应用重写规则:

    RewriteEngine On
    RewriteRule    ^([^/\.]+)/([^/\.]+)$    packagemenu.php?conname=$1&consubname=$2    [NC,L]  
    

    【讨论】:

    • 我可以在暂存网站中测试它吗,因为我的 htaccess 无法正常工作我已经尝试过这个但没有 [NC,L]。我已经尝试过这个 htaccess 代码但它不起作用。
    • @RushabhShah 它对我有用。我不知道你以哪种方式不起作用。您是否在 URL 中输入了http://somesite.com/domestic-tours-packages/kerala-tours-packages
    • 嘿,谢谢!有效!!但是css和js没有加载!
    • 如果有多个页面如 packagemenu.php 然后 somepage.php 怎么办?我试过 RewriteRule ^([^/\.]+)$ packagedetails.php?pid=$1 [L] RewriteRule ^([^/\.]+)/([^/\.]+)$ packagemenu.php ?conname=$1&consubname=$2 [NC,L]
    • 其实这是另一个问题,需要提供在什么情况下需要重定向到packagedetails,在什么情况下需要重定向到packagemenu。
    【解决方案2】:

    如果您想从查询字符串重定向到短网址,请尝试以下操作

     RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /packagemenu.php\?conname=([^&]+)&consubname=([^\s]+) [NC]
    RewriteRule ^ /%1/%2? [NC,L,R]
    
    #skip the rule if the request is for dir
    RewriteCond %{REQUEST_FILENAME} !-d
    
     #skip the rule if the request is for file
     RewriteCond %{REQUEST_FILENAME} !-f
     #rewrite any other request  to "/packagemenu.php?" 
     RewriteRule ^([^/]+)/([^/]+)/?$ /packagemenu.php?conname=$1&consubname=$2 [NC,L]
    

    最简单的方法就是如上!

    【讨论】: