Redirect with nginx (remove substring from url) - nginx

I want do a redirect from old url:
http://example.org/xxxxxxxxx.html
To new urls (remove ".html")
http://example.org/xxxxxxxxx
How I can do this with nginx?
EDIT:
xxxxxxxxx can be differ, example:
http://example.org/url-1.html redirect to http://example.org/url-1
http://example.org/another-url.html redirect to http://example.org/another-url

location ~ ^(.*)\.html$ {
return 301 $1;
}

Probably you need a rewrite statement
location /xxx.html {
rewrite ^/xxx(.*) http://example.org/xxxxx permanent;
}
You detailed explanation please refer https://www.nginx.com/blog/creating-nginx-rewrite-rules/
Another method would be return directive
server {
listen 80;
listen 443 ssl;
server_name www.old-name.com old-name.com;
return 301 $scheme://www.new-name.com;
}

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.org www.example.org;
return 301 http://$server_name$request_uri;
}

Related

404 a directory in nginx server block

In my /conf.d/ folder I have a .conf file as follows:
server {
listen 80;
listen [::]:80;
return 301 https://www.example.com$request_uri;
}
You can see that it redirect any http traffic to https, that is fine.
However, there are certain http URLs, like http://www.example.com/profile/(.*) that I want to 404 immediately, without the redirect to the https version first.
I have tried variants of the following code with no success, the URL, eg http://www.example.com/profile/abc123, is always redirected to the https version.
server {
listen 80;
listen [::]:80;
location ~* ^/profile/ {
return 404;
}
return 301 https://www.example.com$request_uri;
}
Note that I want to capture example.com/profile/ and any pages that match that, eg example.com/profile/abc123 etc
Thank you.
server {
listen 80;
listen [::]:80;
location ~ ^/profile/ {
return 404;
}
server_name example.com *.example.com;
location ~ ^/ {
return 301 https://www.example.com$request_uri;
}
}

Nginx configure root domain redirect to subdomain

my current setup of webpage is:
forum.xyz.pl
I need xyz.pl redirect to forum.xyz.pl
current nginx.conf:
nodebb.conf
I am using aws route53, not sure what value should I put there for root domain also.
thanks
pl to forum.xyz.pl you can simply do:
server {
server_name xyz.pl;
rewrite ^ forum.xyz.pl$request_uri? permanent;
}
This should solve your problem, let me know if you have any other problems. I don't really understand the problem with Route 53 since it is just handling the DNS entries.
I'd do it like this
server {
listen 80;
server_name xyz.pl;
return 301 https://forum.xyz.pl/;
}
server {
listen 80;
server_name forum.xyz.pl;
#Force Https
return 301 https://$host$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
#listen [::]:80;
#listen 80;
server_name forum.xyz.pl;
##rest of config goes here
}

Nginx Redirect entire domain to specific URL

I am attempting to redirect an entire vanity domain to a specific URL for our main domain at the NGINX level. I have tried all of the following, but in all cases the domain will redirect not to the specific page but to the top level domain.
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
return 301 https://myrealurl.com/internal/landingpage/;
}
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
location / {
return 301 https://myrealurl.com/internal/landingpage/;
}
}
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
rewrite ^/$ https://myrealurl.com/internal/landingpage/ permanent;
rewrite ^/(.*)$ https://myrealurl.com/internal/landingpage/ permanent;
}
I've tried each of these individually and all did the same thing, redirect me to https://myrealurl.com/ dropping the internal/landingpage/.

Nginx server block

Hy everyone,
I have a little problem with nginx server block and I hope that someone from here will know the solution to it.
This is how my configuration looks like:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com;
}
server{
listen 80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name .example.com;
...
}
So the first block is normal http redirecting to https second is catching subdomains and third is where location blocks are etc.
My problem is that when the second block catches the request would like to add /admin to URL so it would be subdomain.example.com/admin but i have to check if it already has /admin so i don't get something like this subdomain.example.com/admin/admin.
I tried server_name *.example.com$ and server_name ~.example.com(=<id>.*) then if ($id = '') and hundreds of combinations and didn't get anything usefull.
Did anyone here had similar problem and solved it?
server {
listen 80;
server_name *.example.com;
if ($request_uri !~ ^/admin/) {
return 301 https://$host/admin$request_uri;
}
return 301 https://$host$request_uri;
}

nginx www redirect except other subdomains

Probably yet another nginx redirect question... couldn't find the answer, so:
How do I redirect http://domain.com to http://www.domain.com but do not rewrite any subdomain http://*.domain.com given the cloudfoundry nginx configuration which contains this server definition:
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
I tried this configuration
server {
server_name domain.com
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
but am getting infinite redirects.
Try replacing
server_name _;
with
server_name *.domain.com;
server {
listen 80;
server_name domain.com
return 301 http://www.domain.com$request_uri;
}
server {
listen 80 default_server;
...
}
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html

Resources