How to rewrite /file/(name) to /q.html?id=(name) in NGINX - nginx

For example, rewriting /file/example to /q.html?id=example or /file/test to /q.html?id=test using NGINX.
How would that look like?

There are a number of ways of preforming an external redirect in Nginx, but the simplest solution would be:
rewrite ^/file/(.+)$ /q.html?id=$1 redirect;
See this document for details.

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

NGINX url rewrite - remove /web/guest/

I need to remove /web/guest/ from all urls on a portal via nginx.
Currently urls looks like this:
www.mywebsite.com/en/web/guest/blog-information
www.mywebsite.com/en/web/guest/something-else/information2
www.mywebsite.com/en/web/guest/blog-information3
and so on....
Should be:
www.mywebsite.com/en/blog-information
www.mywebsite.com/en/something-else/information2
www.mywebsite.com/en/blog-information3
and so on....
What should I add in nginx.conf in order to make this change work ?
This can be done with nginx rewrite, try add a rule like this:
rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent;
This will remove the last /web/guest/ in your uri, you can write a more specific rewrite rule depending on your situation.
The last parameter given above is an optional flag, permanent is for 301 redirection and redirect is for 302, there are also other options so you'd better read the docs for more detailed information.

Nginx Rewrite Not rewriting the url

I have tried this for hours and can't seem to get it. I copied other examples and still can't get rewrite to work.
I need my url on nginx to look like http://myurl.com/main/login, http://myurl.com/somethigelse/home, est. Any Help Appreciated. I'm new to nginx, seems a lot faster.
My nginx rewrite looks like this:
rewrite ^/([^/]+)/([^/]+)$ /index.php?db=$1&action=$2 last;
rewrite ^/([^/]+)/([^/]+)/$ /index.php?db=$1&action=$2 last;
It works for me. Do you tell nginx to reload the configuration (do a sudo service nginx reload)? Otherwise, nginx will still be using the old config.
Note, that you can use one line by making the final slash optional using a question mark:
rewrite ^/([^/]+)/([^/]+)/?$ /index.php?db=$1&action=$2 last;
# ^

Rewrite url with parameters in Nginx

i need rewrite this url in nginx, from:
http://domain.com/index.php?src=pictures/2014/11/23/1ZWi3tmM.jpg&w=330&h=186&a=c
to something like this
http://domain.com/thumb/300x186/ac/pictures/2014/11/23/1ZWi3tmM.jpg
i tried with this rule
rewrite /thumb/([0-9.]+)x([0-9.]+)/ac/([A-Za-z0-9.]+) index?src=$1&w=$2&h=$3&a=c;
but no luck. Any suggestion?
I've no experience with nginx, sad but true. Thank you

nginx: Redirect special characters like "%23" in URL to "#"

My nginx site has a few bad links pointing to it like this:
/some-page%23some-part
/some-page">
This is causing 404's and Google Webmaster Tools is complaining too.
The URL /some-page#some-part does get processed properly and works.
How can I get nginx to redirect the %23 in a URL to #? What about the "> junk?
The links out there cannot be changed, so I'm looking to 301 redirect them myself.
Thanks!
Edit: thanks to Deadooshka for the help. My working solution, rewrite ^(.*)\#(.*)$ /$1#$2 redirect;, is discussed within his answer's comment thread.
not tested. I'm not sure which symbols get the pattern.
rewrite ^/([^\#]+)\#([^\#]+)$ /$1#$2 redirect;
rewrite '^/([^\&]+)\&quot\;\&gt\;$' /$1 redirect;

Resources