dragonhong641016 2011-03-11 18:09
浏览 4
已采纳

使apache2提供具有名称等查询字符串的文件

I have a couple of files named like "index.php?page=1", "news?id=1". How can I set up apache to send exactly these files with queries like "http://site.com/index.php?page=1", "http://site.com/news?id=1" etc. I tried .htaccess rules

RewriteRule ^(.*)$ $1 [L,QSA]

It does not work.

  • 写回答

1条回答 默认 最新

  • dougang1967 2011-03-11 18:12
    关注

    You have to encode the special characters ? and = in the URL that's used for the request.


    For example, if your file is called strange.php?plop=1, then, your URL must be :

    http://tests/temp/strange.php%3Fplop%3D1
    

    In PHP, this can be done using the urlencode() function.



    Edit after the comment : in this case (which is really not good... ), a solution might be to use a rewrite rule like this one :

    RewriteRule .*    temp.php
    

    This will redirect everything to temp.php.

    There, you might be able to work with something in $_SERVER, to find what was the request.


    For example, if I call this URL :

    http://tests/temp/strange.php?plop=1
    

    And use this portion of code in my temp.php :

    var_dump($_SERVER);
    

    I get this output :

    array
      ...
      'SERVER_NAME' => string 'tests' (length=5)
      'SERVER_ADDR' => string '127.0.0.1' (length=9)
      'SERVER_PORT' => string '80' (length=2)
      ...
      'REDIRECT_QUERY_STRING' => string 'plop=1' (length=6)
      'REDIRECT_URL' => string '/temp/strange.php' (length=17)
      ...
      'QUERY_STRING' => string 'plop=1' (length=6)
      'REQUEST_URI' => string '/temp/strange.php?plop=1' (length=24)
      'SCRIPT_NAME' => string '/temp/temp.php' (length=14)
      ...
    

    So, here, I guess I could re-build the file-name, using $_SERVER['REQUEST_URI'].

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

报告相同问题?