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;
}
}
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;
}
I'm trying to rewrite :
http://example/test/ -> http://example/new/
http://example/test/check -> http://example/new/check
location ~/test/(.*)$ {
rewrite ^/new/$1?$args permanent;
}
What am I doing wrong?
Your rewrite statement is incorrect. See this document for more.
To use rewrite to capture the latter part of the URI, try:
rewrite ^/test/(.*)$ /new/$1 permanent;
Alternatively, to use location to capture the latter part of the URI, try:
location ~ ^/test/(.*)$ {
return 301 /new/$1?$args;
}
I'm using Nginx 1.6.2. I read that if () is evil and it's not good using it so I need a bit help, because I can't do what I want without using if(). I will post the rules I have with if and would ask if somebody could help me and tell me how to not use if () and use something else and get the same result.
# REDIRECT NON-WWW TO WWW.
if ($http_host != "www.site.eu") {
rewrite ^ http://www.site.eu$request_uri permanent;
}
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
# REMOVE ANY MULTIPLE SLASHES IN THE URL.
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) $scheme://$host/$1 permanent;
}
First rule should be replaced with separate server blocks
server {
listen 80 default_server;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
# normal config
}
Other ifs usually are not necessary. Just don't generate links with index.php and you will not need to strip it.
In the official wiki introduction it says that there are some cases which are ok. Have a look at this quote:
The only 100% safe things which may be done inside if in location
context are:
return ...; rewrite ... last;
At the end of the introduction there is an example which also features a rewrite command. So your code looks ok, too.
EDIT: You should also have a look at how the if works.
You can replace this block
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
with this
location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
I also don't think you need to worry about the last rule for double // because nginx by default automatically takes care of that before it even gets to the point of matching location blocks
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;
}
I'm configuring an Nginx server with both http and https services. I'm trying to achive the following configuration:
redirect every page to HTTPS, except for the home page
In my "http" server configuration, I have already the second rewrite condition working, but I cannot find the way to write the first.
location = / {
what goes here???
}
location / {
rewrite ^(.*) https://mydomain.com$1 permanent;
}
Ideas?
Zenofo's answer should mostly work (just needs the regex "!~*" instead) but will redirect requests that include the name of the home page along with the others.
Using "$uri" in place of "$request_uri" and spelling out the home page file name in the regex gets around this.
location / {
if ($uri !~* ^/index.html)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}
If running php where everything goes through index.php (front end controller) then you can use
location / {
if ($uri !~* ^/index.php$)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}
using $request_uri,like this: (I haven't tested)
location / {
if ($request_uri != ^/$)
{
rewrite ^(.*) https://mydomain.com$1 permanent;
}
}