Nginx Simple rewrite - url-rewriting

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;
}

Related

rewrite rule in nginx to remove extra '/' at the end

So I wanted to create a rewrite rule such that when a uses types in
mydomain.com/toplevel/secondlevel/
it rewrites it to
mydomain.com/toplevel/secondlevel
similarly if I have
mydomain.com/toplevel/
I wanted this to be
mydomain.com/toplevel
I understand that this can be done using the rewrite command and regex, but can't seem to find the right regex for it.
Add this rule to main server section:
rewrite ^/(.*)/$ /$1 permanent;
It removes slash at the end from any adress which contains it.

Specific URL-Rewriting with Nginx using location,rewrite and alias

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.

rewriting urls for static js and css files using nginx server

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;

Nginx rewrite properly quoted ampersand in values causing problem

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).

nginx rewrite rule

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.

Resources