Nginx double slash after domain rewrite - linux

i'm using Nginx on production env, we have some url with double slash in uri, like this:
http://foo.com//bar/foo
i want to rewrite or redirect to:
http://foo.com/bar/foo
the option " merge_slashes " it's already enabled, and i have tried this:
merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;
but doesn't works, have you any idea?
thanks
EDIT:
so, the rewrite work if the slash is after the first "/" , i.e:
http://pippo.it/foo//bar
but if the double slash it's after the domain doesn't work, i.e:
http://pippo.it//foo/bar
i have tried to dump the variable $request_uri and this is the result:
url: http://pippo.it//foo/bar
expect: $request_uri -> //foo/bar
result: $request_uri -> /foo/bar
url: http://pippo.it///foo/bar
expect: $request_uri -> ///foo/bar
result: $request_uri -> //foo/bar
can be a bug?
EDIT 2:
i have found the error, the problem is the elb (aws), if i call directly the ec2 instance (where nginx it's installed) i can see the first 2 slashes, but if i call the elb, the load balancer delete the first slash, i have opened a case on the support center.
EDIT 3:
found the issue: if you set the elb with HTTP listener, i dont know why but the request doesn't have all slashes, if you set the listener in TCP mode, the request works fine.
Thanks

Setup is actually correct. Tested it with nginx/1.9.12
root#8317e542a878:/etc/nginx/sites-enabled# curl -I localhost//
HTTP/1.1 301 Moved Permanently
Server: nginx/1.9.12
Date: Mon, 18 Jul 2016 20:34:29 GMT
Content-Type: text/html
Content-Length: 185
Location: http://localhost/
Connection: keep-alive
Have you reloaded settings on running nginx?
nginx -s reload

Give this a try:
echo 'http://foo.com//bar/fool' | sed 's/\/\//\//g'
This is using the sed unix command to search for // and replace them with /.

Related

RewriteRule header Content-Type: image/jpeg

My site, parsing image from another site, but don't show them. (It shows: ����������������� )
I know what it is, it is a problem with Content-Type.
I need to use Content-Type: image/jpeg, and all will be ok!
But images are opened by URL (eg. example.com/DIR1/(100000-999999)/DIR2/(100000-999999)/). This number is random.
If I write in .htaccess (on example.com) the following then it works:
Header set Content-Type: image/jpeg - i see photo .
But then all other URLs fail!
How can I : If DIR1/(.*)/DIR2/(.*)/ - Use Content-type: image/jpeg ?
DIR1 , DIR2 = Photos/(number)/image/(number)
Your script that serves the image should really be generating this header as part of the response.
However, to set this conditionally in .htaccess just for this form of URL-path then you could do something like this:
# Set env var if required URL-path is requested
SetEnvIf Remote_URI "^/DIR1/\d+/DIR2/\d+/$" SET_JPG_CONTENT_TYPE=1
# Only set header when env var is set
Header set Content-Type "image/jpeg" env=SET_JPG_CONTENT_TYPE

nginx regex redirect gives extra slash

I plan to convert this address:
www.somewebsite.com/news/22915
To this address:
www.somewebsite.com/news/22915.html
And this is how I do it in my nginx configuration:
rewrite ^/news/(\d+)$ /news/$1\.html redirect;
But somehow, nginx will rewrite my address to:
www.somewebsite.com/news/22915/.html
Had no idea why / will go into the variable automatically, since (\d+) should only be matching pure numbers, need help.
Use rewrite ^/news/(\d+)$ /news/$1.html redirect;
The second part of the rewrite syntax is not a regex. So you should not try to escape the dot character.

nginx rewrite rule php melody

I am using nginx. I want to rewrite urls. My code:
rewrite ^/([^/]*)_([a-zA-Z0-9]{9}).html$ /watch.php?vid=$2 last;
Example of url:
http://104.238.130.170/hudson-against-the-grain-video_14a4e06f8.html
But when I save file then restarts nginx server i got error:
[emerg] directive "rewrite" is not terminated by ";" in /etc/nginx/conf.d/default.conf:46
QUESTION: What is wrong on my rewrite rule?
rewrite ^/([^/]*)_([a-zA-Z0-9]{9}).html$ /watch.php?vid=$2 last;
The curly brackets {9}are most likely giving a problem in your regex. Surround the rule with quotes like below and try it.
rewrite "^/([^/]*)_([a-zA-Z0-9]{9}).html$" /watch.php?vid=$2 last;
Note: for curly braces( { and } ), as they are used both in regexes
and for block control, to avoid conflicts, regexes with curly braces
are to be enclosed with double quotes (or single quotes).
More info here

Remove parameters within nginx rewrite

I'm rewriting URLs in nginx after a relaunch. In the old site I had query parameters in the URL to filter stuff e.g.
http://www.example.com/mypage.php?type=4
The new page doesn't have these kind of parameters. I want to remove them and rewrite the URLs to the main page, so that I get:
http://www.example.com/mypage/
My rewrite rule in nginx is:
location ^~ /mypage.php {
rewrite ^/mypage.php$ http://www.example.com/mypage permanent;
}
But with this rule the parameter is still appended. I thought the $ would stop nginx from processing further values... any ideas? All other questions deal with how to add parameters - I just want to remove mine :)
Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.
If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)
So for your example, this would do the trick:
location ^~ /mypage.php {
rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}
To drop a parameter from a URL, in this case coupon=xxx:
if ($query_string ~ "^(.*)coupon=(.*)$") {
rewrite ^(.*)$ $uri? permanent;
}
Note that this will drop all parameters if the
statement matches. $uri is the original request without parameters.
Try setting the $args variable to empty inside the location.
set $args '';
If you want to remove a specified parameter from url,
# in location directive:
if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
set $original_path $1;
set $args1 $2;
set $unwanted $3;
set $args2 $4;
set $args "";
rewrite ^ "${original_path}?${args1}${args2}" permanent;
}
then visit your_site.com/a=1&unwanted=2&c=3
step1. server gives an 302 response, indicating the url is match.
step2. client re-send a request with the new url ( with the parameter removed)
If we append a ? to the end of the destination it will prevent the query from being appended to the destination. :)
Example
Source
Args
Destination
Keep the arg appended
rewrite ^/mypage.php$
arg=something
https://example.com/new-page/
Do not preserve the arg
rewrite ^/mypage.php$
arg=something
https://example.com/new-page/?

Redirect 301 cgi-bin/ to new URL

it's my first question here :)
I got a problem for redirecting URL:
I have old URL like www.domain.com/cgi-bin/category.cgi?type=...
And try to redirect them to www.domain.com on the htaccess
but I still have 404 error...
This is my rule :
RewriteRule ^cgi-bin/(.*)$ http://www.domain.com [R=301,L]
I verified if there are something in the conf about cgi-bin but nothing.
I did a test with "cgi-bin2" and it works...
So what can i do ?
I don't know where you problem come from but why don't you try to write a perl script which will redirect to your base domain url ?
(it can work if you have, for example, just few cgi files previously used).
In your example it seems you want to redirect "category.cgi".
so, in our case, write a "category.cgi" file in your "cgi-bin" folder and write this code inside it :
#!/usr/bin/perl
#
# fixedredir.cgi
use strict;
use warnings;
my $URL = "http://www.yourdomain.com/";
print "Status: 301 Moved\nLocation: $URL\n\n"
Hope it help !

Resources