I am trying to redirect http://example.com/test/index.php?/Server/Search/IP_ADDRESS to http://example.com/?ip=IP_ADDRESS
Tried following rewrite rule, but, it is not working.
rewrite ^/test/index.php?/Server/Search/(.*)$ http://example.com/?ip=$1 redirect;
This is needed for internal purpose as another api will check the IP address at http://example.com/?ip=IP_ADDRESS
location = /test/index.php {
if ($args ~ "^/Server/Search/(.+)") {
set $sip $1;
rewrite ^(.*)$ /?ip=$sip? redirect;
}
}
Related
I have a MantisBT installation that I want to redirect to GitHub. For specific issues, there exists a corresponding issue at GitHub, so I want to treat those 'view' URIs first. Then all other URIs should just go to 'issues'.
# send the bare domain to GitHub issues
rewrite ^/$ https://github.com/Slicer/Slicer/issues/ permanent;
# https://issues.slicer.org/view.php?id=4725 => https://github.com/Slicer/Slicer/issues/4725
location ~ ^/view.php {
# rewrite ^.*$ https://github.com/Slicer/Slicer/issues/$arg_id permanent;
return 301 https://github.com/Slicer/Slicer/issues/$arg_id;
}
# Send all remaining URIs to github issues
rewrite ^ https://github.com/Slicer/Slicer/issues permanent;
The problem I'm having is that the view.php rule is not working properly. I'm getting
https://github.com/Slicer/Slicer/issues?id=1234 instead of
https://github.com/Slicer/Slicer/issues/1234
I simplified my configuration to the following, and it's working now as intended.
# View URIs like https://issues.slicer.org/view.php?id=4725
location ~ ^/view.php {
# rewrite ^.*$ https://github.com/Slicer/Slicer/issues/$arg_id last;
return 301 https://github.com/Slicer/Slicer/issues/$arg_id;
}
# everything else
location ~ (?!view) {
rewrite ^(.*)$ https://github.com/Slicer/Slicer/issues? permanent;
}
Current API end points are like
domain.com/api/rest/[actual_module_routing_path, eg. cms/page/home]
In the new system this has to be something like
domain.com/rest/V1/$1
It has to work regardless of HTTP Method (GET, POST, PUT, DELETE). Trying something like this in nginx host config and I couldn't get it working, some help will be really appreciated
location /api/rest {
#Rewrite $uri=/api/rest/ back to just $uri=/rest/V1/$1
rewrite ^/api/rest/(.*)$ /rest/V1/$1 break;
}
location ~ /api/rest/(.*)
{
rewrite ^(.*)$ /rest/v1/$1 redirect;
}
I just want to redirect / to a different file rather than index.php/.html.
So on apache I can do this..
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . /path/to/dir/index.html [L]
Trying this in nginx just doesn't even get run, it seems:
if (-f $request_filename){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite /. /path/to/dir/index.html last;
}
Also tried this:
if ($request_filename ~ "/index.php"){
set $rule_3 1$rule_3;
}
if ($args ~ "^$"){
set $rule_3 2$rule_3;
}
if ($rule_3 = "21"){
rewrite /. /wp-content/plugins/i-static/content/index.html last;
}
It acts like it doesn't even exist and just prints out index.php.
If you want to isolate the single URI /, use the location = variant. Try:
location = / { rewrite ^ /new/index.html last; }
This will perform an internal rewrite. See this document for details.
I have a nginx rewrite rule like this
location ~* /question\-(.*)\.html$ {
rewrite "^/question-([0-9]+).html$" "/question/$1.html";
rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
}
This rule mean is:
if URI is /question-123456.html so rewrite to /question/123456.html
/question/123456.html is static file, and via question.php to create.
So when I visit http://example.com/question-123456.html HTTP rewrite to http://example.com/question/123456.html if not exist, I want to execute next rewrite rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
Other than return 404 to user.
I would write it this way:
location ~ /question-(.*)\.html$ {
try_files /question/$1.html /question.php?id=$1;
}
I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}