Rewrite a rule in nginx so that every request that comes in goes to the root `/` - nginx

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

Related

Nginx redirect path to a new structure not working with "#"

I'm trying to create the following rewrite in nginx:
https://my-domain/app/kibana#/discover?<some-get-params>
to
https://new-domain/app/discover#/?<some-get-params>
I'm struggling I think because of the "#" character.
I am able to redirect to the destination, but always without the params.
My last attempt was as follows:
location / {
rewrite ^/app/kibana#/discover(.*)$ https://my-new-domain.com/app/discover#/$1 redirect;
}
What happens with this one is a 404.
If instead I do something like:
location / {
rewrite ^/app/kibana(.*)$ https://my-new-domain.com/app/discover#/$args redirect;
}
I get redirected to the right destination but without any args: https://my-new-domain.com/app/discover#/
Same thing if instead of $args I use $1 instead

Permanent redirection between folders with nginx?

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

nginx rewrite rule with query parameter

My URL looks like:
localhost/video-detail?videoID=T0r-uCXvDzQ
I want to serve a page with name: T0r-uCXvDzQ.html (videoID.html) which is present in server's file system.
I am trying to write the rewrite rule as follows:-
location / {
rewrite ^/video-detail?videoID=(.*) /$1.html;
}
Also tried:
location / {
rewrite ^/video-detail?videoID=(.*) /$arg_videoID.html;
}
But they are giving 404 error.
How can I use the query parameters in the output rewrite rule.
The following worked for me:-
if ($args ~* "videoID=(.*)") {
set $key1 $1;
rewrite ^(/video-detail)$ /$key1.html;
}

How to write this nginx rewrite rule

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

Rewrite configuration issue in a Nginx server

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

Resources