Make apache2 to serve files with query string like names
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", "htt开发者_开发百科p://site.com/news?id=1" etc. I tried .htaccess rules
RewriteRule ^(.*)$ $1 [L,QSA]
It does not work.
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']
.
精彩评论