I am trying to rewrite some urls with following nginx rewrite rule
rewrite ^/some\/url\/(.*)\/$ /some/url/?filter=$1;
Rewriting does not seem to work if a query contains an & e.g.
?filter=key:abcd & efgh
which I am quoting properly to
?filter=abcd%20%26%20N%20efgh
My problem is only rewritten url /som/url/key:abcd%20%26%20N%20efgh do not work. If I access it like /some/url/?filter=abcd%20%26%20N%20efgh it works fine.
Am I missing something?
Thanks.
Putting URL you want to rewrite to another "location" could solve your problem.
(I suppose in location directive nginx doesn't do any escaping)
So for your example it would be:
location /some/url/ {
if ($uri ~* ^/some/url/(.*)$
{
rewrite ^.*$ /some/url/?filter=$1 break;
}
proxy_pass http://127.0.0.1:8080;
#proxy_set... <- and other proxy related things
}
at least it worked for me(in my case even question marks were replaced with "%3f".
Excerpt from official documentation "Note that the $args variable is not decoded, unlike URIs during location matching." http://wiki.nginx.org/HttpRewriteModule
I noticed that I was using an old version of the nginx(7.6) and the problem was solved by upgrading to the latest stable release(1.0).
Related
Currently I arrange my /var/www directory with each site in its own folder named after the hostname (e.g. /var/www/www.example.com). When I want to also cater for the non-www version (or something else) I just symlink to the proper directory (so /var/www/example.com symlinks to /var/www/www.example.com).
I want to take this a step further and have apache do a permanent redirect to the non-symlinked directory. I've come up with the following RewriteMap perl script:
#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
chomp;
if (-l "/var/www/$_") {
print readlink . "\n";
} else {
print "NULL\n";
}
}
I've never perled before so forgive any errors. I've tested it and it works well and conforms to the spec.
The hard part is the actual rewrite rules. I've tried this and it's as far as I've got. I'm suspecting that I need to have a condition that checks if there is a match.
OK, so in the process of writing this question I figured out a few things.
Firstly one needs to supply a newline character after NULL from whatever script you're running. I've put that in the question. We then check if the match isn't an empty string and proceed from there:
VirtualDocumentRoot /var/www/%0
RewriteEngine On
RewriteMap symlinks prg:/var/www/redirector.pl
RewriteCond ${symlinks:%{HTTP_HOST}} !=""
RewriteRule ^/(.*) http://${symlinks:%{HTTP_HOST}}/$1 [R=301,L]
My solution ignores SSL but it's likely that you'll override the default catchall virtual host for your SSL sites to add key's etc...
I am a beginner with Nginx and I've a question with a specific URL Rewriting.
I want "http://myserver.fr/context/[xxxxxxxxxxx]/synchronize.php" to redirect to /space/www/synchronize.php
And I want another URL type "http://myserver.php/context/[xxxxxxxx]" to redirect to /space/www/index.php
So I think the correct conf Nginx is :
location /context/ {
alias /space/www/;
rewrite ^(.*).synchronize.php /synchronize.php last;
rewrite ^/(.*)$ /index.php last;
}
As I am a beginner in Nginx, can you tell me if my idea is logical or not? Thanks a lot.
i would like to rewrite js and css files using nginx
i have this urls pattern
css :
http://myhost.com/css/min/css_home.1330004285.css
js :
http://myhost.com/js/min/js_home.1330004285.js
for the css files have to redirect to
http://myhost.com/css/min/css_home.css and the same way for the js files
i tried to resolve this by using thi solution but i doesn't work , it showing me an error when restarting nginx server
location ~* \.(css|js) {
rewrite /(.*)\.[\d]{10}\.(css|js) $1.$2 last;
}
The rewrite rule seems a bit over complicated.
You can try this:
rewrite /(.+/)\.+\.(css|js)$ /$1.$2 last;
If you have to use your original one, you need to wrap it in quotes because it includes curly braces ... '{' and '}'
rewrite "/(.+)\.[\d]{10}\.(css|js)$" /$1.$2 last;
I want to make the following url in nginx
comments.php?id=34
becomes
/comments/34
/comments/34/
I am trying with this and it works
rewrite ^/comments/$id/(.*)$ /comments.php?id=$1? last;
My Question is, how to do I force redirect comments.php?id=x to /comments/id
rewrite ^/comments.php$ /comments/$arg_id? permanent;
According to the documentation, "rewrite operates only on path, not parameters."
Try this instead:
if ($args ~ id=(.+)){
rewrite comments\.php /comments/$1 last;
}
HI,
Given the following apache/mod_rewrite rule taken from .htaccess within minify directory on the server:
RewriteRule ^([a-z]=.*) index.php?$1 [L,NE]
what will be the nginx compatible equivalent of it? I have tried:
rewrite ^/minify/([a-z]=.*) minify/index.php?$1 break;
but that doesn't seem to work.
Any ideas guys?
I have something like this and it's working for me
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
Try something more like bbPress's code.