Rewrite rule for the root file in nginx? - nginx

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.

Related

How do I redirect a domain and specific URIs with NGINX?

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;
}

Nginx rewrite not working as per docs

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;
}
}

Nginx rewrite to include folder

I'm having a problem with my rewrite rule. It doesn't include folders in the rewrite path. For example:
/randomstring/app.js rewrites to /var/www/CDN/Dev/App/app.js
/randomstring/dashboard/app.js rewrites to /var/www/CDN/Dev/App/app.js but it should rewrite to /var/www/CDN/Dev/App/dashboard/app.js
I don't understand why it doesn't work. (.*) matches everything but a dot if I'm not mistaken so why doesn't it include the dashboard/ part?
location ~* (css|js)$ {
rewrite ^/([^/]*)/(.*).(css|js)$ /$2.$3 ;
root /var/www/CDN/Dev/App;
}
I see no reason to use rewrite here. Alias should be enough
location ~* /[^/]+(/.+\.(css|js))$ {
alias /var/www/CDN/Dev/App/$1;
}
location ~* \.(css|js)$ {
rewrite ^/([^/]+)/(.+)\.(css|js)$ /$2.$3 ;
root /var/www/CDN/Dev/App;
}

how do I rewrite a URL in nginx to an updated location?

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;
}

Nginx Rewrite: change only index location?

How can I send a user to a new location, only if the user have no URI? I am trying the follow, but it does not works... it always send me to /newlocation
rewrite ^/$ http://www.domain.com/newlocation permanent;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
So basically, what I need is:
If the user writes on browser www.domain.org it sends to www.domain.com/newlocation
If the user writes on browser www.domain.org/something it sends to www.domain.com/something
Thanks!
I'm not sure why your current approach isn't working. ^/$ should only match /. Maybe it's something else it the current config. Here's a server that should do what you want.
server {
server_name www.domain.org;
# Only match requests for /
location = / {
rewrite ^ http://www.domain.com/newlocation permanent;
}
# Match everything else
location / {
rewrite ^ http://www.domain.com$request_uri? permanent;
}
}

Resources