Rewrite path without extension to include extension - nginx

I'm trying to rewrite http://localhost/az to http://localhost/az.vcf (which exists as a file I want to be able to download.
I tried:
server {
server_name localhost;
root /www/data;
rewrite ^(/.*) $1.vcf last;
}
types {
text/x-vcard vcf;
}
I can download the file but it has no .vcf extension.

Rewrite mechanism works only with internal $uri variable. It does not affect URL in the browser address bar so it can't affect the downloaded file name. If you don't want to do a HTTP 301 redirect
rewrite ^(/.*(?<!\.vcf))$ $1.vcf permanent;
or HTTP 302 redrect
rewrite ^(/.*(?<!\.vcf))$ $1.vcf redirect;
you can try to add the Content-Disposition header to your response:
server {
server_name localhost;
root /www/data;
rewrite ^(?<path>.*/)(?<name>[^/]*) $path$name.vcf last;
add_header Content-Disposition 'attachment; filename="$name.vcf"';
}

Related

Nginx Rewrite Encoded URI (with percent) Not Working

I have a WordPress site that needed to 301 redirect old post to new post.
Old post:
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
New Post:
https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/
I added this rule in nginx.conf for this domain here
server
{
listen 111.222.333.444:80;
server_name example.com www.example.com ;
return 301 https://www.example.com$request_uri;
}
server
{
rewrite_log on;
rewrite ^/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/$ https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/ permanent;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
(the rest of location blocks continue)
}
Restart Nginx.
However, the old URL still return 404 and not a 301.
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
And I don't see neither old nor new URI in error log at all. What should I do? Thanks!
The percent encoded URL is available in the $request_uri variable. But by the time Nginx is processing rewrite and location statements, the URL has been decoded and normalised.
Use a rewrite or location statement with the decoded values. For example:
rewrite ^/สวัสดี/$ /ลาก่อน/ permanent;
Or:
location = /สวัสดี/ {
return 301 /ลาก่อน/;
}

How to edit url without updating in browser - nginx config

I want the Request URL to be converted from
http://host.com/newname/abc?def= to
http://newname/abc?def=
here is the config file
server {
listen 80;
server_name
host.com
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
try_files /$host/public/$uri #webserver;
}
}
Adding above line worked form me
location / {
rewrite ^ $scheme://$request_uri? permanent;
}
But it replaces the url in user browser which i donn't want to happen.
Any way to achieve it
You can try this:
location /newname{
proxy_pass http://example.com/newname/abc?def=;
}
or
location /newname{
proxy_pass http://newname/abc?def=;
}

Nginx Rewrite Rule for the nested location

I have my domain config file setup for Nginx. I have set domain like www.example.com and I have all sub directory also inside that like basefolder/index.html, basefolder/abc1/signup/index.html, basefolder/abc2/signup/index.html, basefolder/signup/index.html etc as n number can be exists.
I can directly call them through domain/dir/ which will call the index.html file available in that directory. I have some specific query string set for the "signup" folders only like "www.example.com/signup/?query=a12seddf" or "www.example.com/abc1/signup/?query=a12seddf". I want to covert them to the path like "www.example.com/signup/a12seddf" or "www.example.com/abc1/signup/a12seddf". I tried with the rewrite rule but it works only if I give the whole location till the signup but I want to write the single rule which will work on all signup folder instead of writing the rewrite rule for all.
I tried below code but it won't work for me and giving 404 on accessing the beautify URL.
location ~* signup/ {
rewrite ^/signup/([a-z0-9A-Z]+)/?$ /signup/?query=$1 break;
}
Please find the whole sample working config file given below with names example.com.conf in nginx/sites-enabled folder:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/sites/example.com;
error_page 404 /error_404.html;
location = /error_404.html {
root /var/www/html;
internal;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
rewrite ^/abc1/signup/(.*)$ /abc1/signup/index.html?_l=$1 last;
rewrite ^/signup/(.*)$ /signup/index.html?_l=$1 last;
}
Thanks.

Rewrite directory as parameter

I'm trying to redirect requests from
example.com/abc234 to
example.com/setup.html?s=abc234
So far, I've tried the following, but it seems to always end up either 1) not transmitting the parameter or 2) ending up in an infinite loop (or 404) because it also tries to redirect the redirected request? The request has to be visibly rewritten because I want to pick up the parameter with JS, not PHP.
server {
server_name example.com;
root /var/www/html;
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}
I've also tried various combinations of location / { try_files ...; } or using the absolute URL within rewrite without success.
One technique is to rewrite only URIs that do not match a physical file.
For example:
server {
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 302 /setup.html?s=$uri;
}
}
See this document for more.
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}

NGINX rewrite root url with language suffix

I am trying to add a simple rule to my NGINX configuration whereby the root url www.example.com is always rewritten with a language suffix www.example.com/en/. I have tried this:
server {
listen 80;
server_name www.example.com;
location / {
rewrite ^$ www.example.com/en/ permanent;
}
...
}
But no luck. Where am I going wrong? Also, is it possible to have a condition where NGINX checks if there is an/en/ suffix and if not, adds one?
EDIT
So I was only one character away from what I wanted initially:
server {
listen 80;
server_name www.example.com;
location / {
# needed the / in between the anchor tags
rewrite ^/$ www.example.com/en/ permanent;
}
...
}
There are two normal ways to deal with redirects:
Using a rewrite rule
server {
...
rewrite ^/$ /en/ permanent;
...
}
Note that rewrite rules do not need to be absolute urls, but if they are to be absolute urls they need to include the protocol rewrite /x https://example.com/y;.
There is no need to put rewrite rules like this in a location block.
Using a location block + return 30x
Using a location block requires using an exact match for the url:
server {
...
location = / {
return 301 /en/;
}
...
}
The use of = means the rule will only match requests for the root of the domain, otherwise the rules of location block precedence mean that the location block would be the default for all requests. Return is used to issue a 301 (permanent) redirect.

Resources