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]
    
    点赞 评论

相关推荐 更多相似问题