My URL is: https://example.com/data=123456
I added the following line of code to the index.php file: var_dump($_GET['data']); But this only works if I add a ? character to the URL. (example: https://example.com/?data=123456) This is in the root of index.php.
I tried to add this to the nginx.conf file:
location / {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^(.*)\?(.*)$ $1$2 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
rewrite ^(.*)\?(.*)$ $1$2 last;
include fastcgi.conf;
}
But not working. How to remove the "?" character from URL?
CodePudding user response:
When your URL is https://example.com/data=123456
, the data=123456
is a part of URL path. When your URL is https://example.com/?data=123456
, the data=123456
is a query part of the URL, where query argument data
has a value if 123456
. Check at least the Wikipedia article.
Next, only query arguments can be accesses via $_GET
array. Full request URI (including both path and query parts) is available via $_SERVER['REQUEST_URI']
. You can analyze that variable in your PHP script and skip the rewrite part completely.
If you want to rewrite your URI and made that data
to be accessible via $_GET['data']
variable, it is also possible. However your main mistake is that the rewrite
directive works only with (normalized) path of the given URL and not the query part of it. Nevertheless it can be used to append (or replace) query arguments. To do it you can use
rewrite ^(.*/)(data=[^/]*)$ $1?$2;
(to rewrite only data
parameter) or more generic
rewrite ^(.*/)([^/=] =[^/]*)$ $1?$2;
(to count on every param=value
form of the URL path suffix). You can place this rewrite rule at the server
configuration level.