dongyongyu0789 2016-02-01 16:37
浏览 20
已采纳

使用.htaccess和PHP清理子组的URL

How can I convert an url as below :

http://localhost/series/dynamics/admin/cleanURL/value1?subgroup=value2

to a cleaner URL,

for e.g: http://localhost/series/dynamics/admin/cleanURL/value1/value2

I am using $_GET and have the option of using $_SERVER['REQUEST_URI']. The name of the php file is index.php. I have managed to achieve clean url for the first parameter i.e. value1 but clueless on how to do so for value2

My htaccess looks like this, pretty sure it needs to pass a second parameter but not able to work out how.

 Rewriteengine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 Rewriterule ^([a-zA-Z]+)$ index.php?group=$1

I have tried: ^([a-zA-Z]+)$ index.php?group=$1?subgroup=$2 and few other methods but none of them are working

Please have a look at 2 PHP codes below, I am not sure if "&amp" will do the job as anything apart from "?" take me to a different page.

<li id="nav"<?php if($pageid==$nav['group']){echo'class="active"';}?>><a href="<?php echo $nav['group'];?>"><?php echo $nav['product']?></a></li>


<a href="?subgroup=<?php echo $list['subgroup']; ?>" <?php if($tst1 == $list['subgroup']){echo 'class="active"';} ?> ><?php echo $list['subgroup']."(".$list['contains'].")".'<br/>';?></a>

Can I use "&" instead of "?" in the link for subgroup ?

  • 写回答

1条回答 默认 最新

  • dongyuan1984 2016-02-01 17:43
    关注

    You have a few problems I see. Whenever you need to add another parameter you need to add another capture group ([a-zA-Z]+) in the RewriteRule pattern or it won't know to look for the in the URI. So using Backreference $2 without another capture group in the pattern will be empty because you only have one.

    Also your query string format is invalid. You only use the ? once at the beginning and then the ampersand (&) to add more key/value pairs.

    Something like this is probably what you're after. I changed it a bit to prevent writing the conditions multiple times.

     RewriteEngine on
    
     #if file or directory is real, do nothing
     RewriteCond %{REQUEST_FILENAME} -f [OR]
     RewriteCond %{REQUEST_FILENAME} -d
     RewriteRule ^ - [L]
    
     #rewrite group and subgroup e.g. http://.../value1/value2/
     Rewriterule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?group=$1&subgroup=$2 [L]
     #rewrite group e.g. http://.../value1/
     Rewriterule ^([a-zA-Z]+)/?$ index.php?group=$1 [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据