重定向URL以及更改查询字符串Apache

时间:2016-08-24 11:40:34

标签: php apache .htaccess mod-rewrite localhost

我有两个条件:

  

http://localhost/restexample/api/con/2/

重写

  

http://localhost/restexample/RestController.php?phone=single&id=2

  

http://localhost/restexample/api?number=12345

重定向/重写

  

http://localhost/restexample/RestController.php?phone=all&no=12345

我在第一种情况下得到了适当的回应,但在第二种情况下得不到。

我的.htaccess文件是:

# Turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on

# map neat URL to internal URL
RewriteRule api/con/([0-9]+)/$   "RestController.php?phone=single&id=$1" [nc,qsa]

RewriteCond %{QUERY_STRING} number=(\d+)$     
RewriteRule "^api"  "RestController.php?phone=all&no=%1" [nc,qsa,R=301]

有人请帮忙。

输出第二个文件:

enter image description here

1 个答案:

答案 0 :(得分:1)

请尝试以下规则:

Options +FollowSymlinks
RewriteEngine on

# map neat URL to internal URL
RewriteRule ^api/con/([0-9]+)/?$ RestController.php?phone=single&id=$1 [NC,L,QSA]

RewriteCond %{QUERY_STRING} (?:^|&)number=(\d+)$
RewriteRule ^api/?$ RestController.php?phone=all&no=%1 [NC,L]