du060334 2013-04-26 09:41
浏览 32
已采纳

url查询参数使用htaccess apache给出其他值

I am learning about url rewriting on .htaccess and is there any good book where i can learn.

I building an RESTfull webservices and i am working with pretty urls

Now I wrote below syntax in .htaccess file to achieve other urls.

RewriteRule ([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/users/([0-9a-zA-Z]) users.php?key=$1&format=$2&uid=$3
RewriteRule ([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/users users.php?key=$1&format=$2

Example 1

And url i tried and result are mentioned below

e.g.    http://localhost/site/key/format/users/user_id


http://localhost/rest/r123/json/users/pGQqAMbVQAFx

test.php

<?php print_r($_GET); ?>

And only get values for key and format not uid and value for key is php and i dont know how php value comes into exists

output.

Array
(
    [key] => php
    [format] => json
)

Example 2

http://localhost/rest/r123/json/users/

output

Array
(
    [key] => r123
    [format] => json
)

And is there any good book on url rewriting for beginners other than apache documentation.

  • 写回答

1条回答 默认 最新

  • douxigai8757 2013-04-26 10:02
    关注

    Your rules had few issues. Here is the fixed code:

    RewriteRule /([a-z0-9]+)/([a-z0-9]+)/users/([0-9a-z]+)/? users.php?key=$1&format=$2&uid=$3 [L,QSA,NC]
    RewriteRule /([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/users/?$ users.php?key=$1&format=$2 [L,QSA,NC]
    

    Issues/Fixes in your code:

    1. After users/ you had this regex: ([0-9a-zA-Z]) which will match single character you probably meant ([0-9a-zA-Z]+)
    2. You should use flags L (Last Rule), QSA (Query String Append), NC (Ignore Case) after each Rewrite rule.
    3. Consider using end anchor $ to avoid matching unwanted URIs.
    4. Use / at the start of regex to avoid matching unwanted URIs.
    5. Instead of [A-Za-z0-9] you can use [a-z0-9] and use NC flag as described above.

    Read more about mod_rewritw flags: https://httpd.apache.org/docs/current/rewrite/flags.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度