On the nginx configuration file
default
at
/etc/nginx/sites-enabled/
I am trying to redirect requests made to the folder "home" to another folder "jp".
Following the nginx manual,
I tried the script below. Any ideas as to why this would't work? Thanks.
server{
...
server_name _localhost;
location /home/ {
rewrite www.example.io/home/$ www.example.io/home/jp/ permanent;
}
}
You want a permanent redirection from /home/ to /home/jp/.
The first parameter to a rewrite directive is a regular expression which is matched against a normalized URI, in your case /home/.
You can use a rewrite directive, for example:
location /home/ {
rewrite ^/home/$ /home/jp/ permanent;
...
}
Alternatively, you could use an exact match location with a return statement, for example:
location = /home/ {
return 301 /home/jp/;
}
Related
I want to write a rule in nginx.conf that will rewrite every incoming request (for eg. if its looking for a file under a sub path /subpath/index.js) to /index.js.
I tried the following block of code
`
check if request isn't static file
if ($request_filename !~* .(gif|html|jpe?g|png|json|ico|js|css|flv|swf|pdf|xml)$ ) {
rewrite (^[^?]+[^/?])([^/]*)$ $1/$2 permanent;
}
`
I have an app running at
http://localhost/subfolder/myapp/
How do I create a rewrite rule so that the following urls get redirected to my app?
http://localhost/subfolder/myapp/route1
http://localhost/subfolder/myapp/route2
http://localhost/subfolder/myapp/routeN
...anyother routes after http://localhost/subfolder/myapp/
add this block nginx rules
location ~ /subfolder/myapp/.* {
redirect from here
}
In Nginx you can do rewrites with a return inside your server block:
location ~ /subfolder/myapp/route(.*) {
return 301 http://localhost/subfolder/myapp;
}
This seems to work:
rewrite ^/subfolder/myapp/.+$ /subfolder/myapp last;
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'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?
I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.
Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.
I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}
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;
}
}