We want nginx to permanent redirect url's with a trailing slash to the non slash url. we found:
https://www.scalescale.com/tips/nginx/nginx-remove-trailing-slash/
So we put:
rewrite ^/(.*)/$ /$ permanent;
In the nginx, but the problem is it must not apply to some folders. so we found:
remove trailing slash in nginx with some certain cases ignored
and we changed it to:
rewrite ^/(?!admin)(.*)/$ /$ permanent;
but then the server wouldn't start:
invalid number of arguments in "rewrite" directive in /opt/www/folder/.nginx:5
And: we want 2 folders excluded.
What is the right regex to exclude the folders from the rewrite rule?
Thanks,
Bart
Edit for who comes here by google:
The answer works... only strange thing is that the standard worked without the $1 :
rewrite ^/(.*)/$ /$ permanent;
and now we made the exclude, it didn't work anymore without the $1.... but this works for now:
# remove trailing slashes
rewrite ^/(?!folder1|folder2)(.*)/$ /$1 permanent;
I suspect this is just a typo. That /$ looks like it should be /$1:
rewrite ^/(?!admin)(.*)/$ /$1 permanent;
If you have more that one URI to exclude, try something like
rewrite ^/(?!admin|secure|raw)(.*)/$ /$1 permanent;
nginx uses the same regular expression library as Perl, so you can test this stuff from a command line with
perl -ple 's#^/(?!admin|secure|raw)(.*)/$#/$1#'
and just typing in example URIs.
Related
I'm trying to rewrite some urls which are showing up as 404's but the I can't get the rewrite to work. The Urls look like this /ossobuco-alla-milanese/1451114854360.1451114854360?time=1451114851111. I would like to remove 1451114854360.1451114854360?time=1451114851111 with a rewrite.
In my nginx config I have the following rewrite rule
rewrite "^\/(.*)\/(\d{13}\.\d{13}\?time=\d{13})$" /$1/ permanent;
I tested the regex in 2 online regex tools regex101 and regex pal and it should work but the don't seem to work on my server.
To match query string use $args:
location / {
if ($args ~* "^time=\d+") {
set $args '';
rewrite "^/(.+)/\d+\.\d+/?$" /$1 permanent;
}
}
PS: If you want to match only 13digits.13digits then use:
rewrite "^/(.+)/\d{13}\.\d{13}/?$" /$1 permanent;
In the end its was just a really simple location block and the ? removes the args.
location ~* ^/(.+)/\d+\.\d+$ {
rewrite ^/(.+)/\d+\.\d+$ /$1? permanent;
}
We have URLs that contains #! in their structure. I would like them to be redirected.
From:
/#!/about/
/#!/about/example/
To:
/about/
/about/example/
I tried this rule, but it didn't work:
rewrite ^([\/][#][!][\/])(.+[\/])(.+[\/])$ /$2$3 permanent;
Try this much simpler expression:
rewrite ^/\#!/(.*)$ /$1 permanent;
I'm having a strange behaviour with Apache's LocationMatch directive when there are extra slashes at the beginning of the URL. According to the Apache docs if I'm reading it right this should work:
<LocationMatch ^/appcontext/(a|b)>
SetHandler weblogic-handler
WebLogicCluster apphost01:xxxx,apphost02:xxxx
WLProxySSL ON
</LocationMatch>
However if I type the following URL it is also being forwarded to the backend hosts:
https:// <hostname:port> ////////appcontext/a/
In the Apache docs it clearly states that it should apply the directive only for /appcontext/a/, unless I'm missing something with the regex or there is some issue with the mod_wl plug-in
http://httpd.apache.org/docs/2.2/mod/core.html
"For example, <LocationMatch ^/abc> would match the request URL /abc but not the request URL //abc"
Does anyone have any suggestion to achieve this?
Yes Apache does collapse extra slashes in rewrite rule. You can use THE_REQUEST variable to match and remove extra slashes. Place this in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule ^ %1/%2 [R=302,L,NE]
I have the following rewrite rule in my nginx:
rewrite ^/(.*)/$ /$1 permanent;
to remove trailing slash at the end of any URL. However I wanted to make an exception such that when the URL is /register/ I don't want this rule to be applied. How do I put that into the regex?
You can use a Negative Lookahead.
^/(?!register)(.*)/$
If you don't want register anywhere between such as /exampleregister/, use the following.
^/(?!.*register)(.*)/$
I'm writing rewrite rules in nginx.
I want to rewrite every URI that matches /A/B[anything] to /X/ except /A/B/C[/].
How do I do this?
What I've tried:
if ($request_uri ~ ^/A/B/C/?) {
break;
}
rewrite ^/A/B /X/ permanent;
This rewrites /A/B to /X/, but /A/B[anything] doesn't get redirected, nor does /A/B/C/ stay at /A/B/C/.
rewrite ^/A/B/?$ /X/ permanent;
Should do it
I figured out the issue.
I needed to make the rewrite line
rewrite ^/A/B(.*)? /X/ permanent;
The (.*)? means " and optionally match anything any amount of times."