NGINX url rewrite - remove /web/guest/ - nginx

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.

Related

How to rewrite /file/(name) to /q.html?id=(name) in 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.

nginx rewrite middle of url to another pattern

This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?

Redirect AMP files on nginx

I'm trying to make redirect on nginx but unfortunately it doesn't work.
What I'd like to achieve, it is to redirect amp files on mobile.
What I'd like to do :
from
https://www.example.com/uri-759.html
to
https://www.example.com/uri-759-amp.html
What I did as redirect
if ($mobile_redirect = perform) {
redirect ^(.*)(\.html)$ $1-amp$2 permanent;
}
what I obtain
https://www.example.com/uri-759-amp-amp-amp-amp-amp-amp-amp-amp.html
Does someone have a solution to perform this redirection ?
You can use a negative lookbehind assertion to avoid matching the rewritten URI.
For example:
rewrite ^(.*)(?<!-amp)(\.html)$ $1-amp$2 permanent;
See this document for more.

Nginx: redirect regardless of the url structure

I recently moved a subdomain to my main domain but I also changed the url structure.
Previously I had pages like http://sub.domain.com/companies/my-company-id/year/2012/charts
When moving to the main domain, I removed all the complicated urls to juts get:
http://www.domain.com/companies/my-company
I currently have the following rule:
rewrite ^/companies/(.*)$ http://www.domain.com/companies/$1 permanent; but when someone go on a page like http://sub.domain.com/companies/my-company/2012/charts they get redirect to http://www,.domain.com/companies/my-company/2012/charts and get a 404.
I like to force a redirection to http://www,.domain.com/companies/my-company-id regardless of what's after the my-company-id
Currently the parameter $1 is having the entire URI after /companies, so you are getting redirected to the original path. You should only extract the company-id in $1.
Use this:
rewrite ^/companies/(.*)/(.*)$ http://www.domain.com/companies/$1 permanent;
Here the rest of the URI after company-id will be available in the parameter $2, which is not needed in the rewrite condition.

Nginx Redirect for WooCommerce Checkout Endpoint

I would like to ask how it would be the rewrite rule for nginx for adding to a variable directory at the end of an address a slash. Specificaly i want to solve a 404 error that occurs when i use WooCommerce with nginx - php-fpm.
The slash to be added is at the checkout endpoint and it would look like this
http://xxxx.xx/checkout/xxx?key=wc_order_5702b88b72e77
to rewrite to
http://xxxx.xx/checkout/xxx/?key=wc_order_5702b88b72e77
where the "xxx" after the /checkout/ is the order number and it is a variable.
Thanks in advance.
I think you would want something like this:
server {
# put this before your locations
rewrite ^(/checkout/[a-z0-9]+)$ $1/ permanent;
}
I assume that the order id contains numbers and lower case characters only. You might want to change this by changing the regex. 0-9 match numbers, A-Z uppercase characters and a-z lowercase characters.
You can also put the rewrite in a location block to make it more specific.
location ~ ^/checkout/[a-z0-9]+$ {
rewrite ^(/checkout/[a-z0-9]+)$ $1/ permanent;
}
Additionally, instead of using the permanent flag that permanently redirects requests (301 redirect) you can use the flag last which stops processing the current set and the rewritten request is passed once again to find the appropriate location.

Resources