Nginx: redirect regardless of the url structure - nginx

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.

Related

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?

Rewrite Nginx URLs with Parameters (After a Question Mark)

I have URLS in this format:
/wiki/index.php?title=Widget
/wiki/index.php?title=Blue
/wiki/index.php?title=Red
/wiki/index.php?title=Etc
I want to be able to match any URL that has the pattern "/wiki/index.php?title=" or even just "/wiki/index.php" (but so it will pick up the above URLs with the "?") and then redirect them to simply /wiki/ (all pages that match the above pattern go to the single url /wiki/)
I used to have a Mediawiki install on the /wiki/ directory with a lot of pages in the format above. However now I am running a Wordpress install and it is using the /wiki/ directory. I don't need each rewritten URL to go to a different URL (I know that is difficult as my source URLs have parameters) but right now they all 404 and so I just want to direct to them /wiki/ at least.
Simple URL rewriting
The easiest case (rewrite all /wiki/index.php requests with any arguments) can be done by this config (inside your server block):
location = /wiki/index.php {
rewrite .* /wiki/?;
}
The '?' sign at the end of second rewrite parameter is a trick to completely remove any request arguments. Without it, request /wiki/index.php?title=Widget will be rewrited to /wiki/?title=Widget.
Rewriting only requests matching /wiki/index.php?title=... is a more complex, I don't know how to do it without if construction:
location = /wiki/index.php {
if ($request_uri ~ ^/wiki/index\.php\?title=) {
rewrite .* /wiki/?;
}
}
If you want to generate HTTP 301 or 302 redirect instead of simple URL rewriting, you can use redirect (for 301 temporary redirect) or permanent (for 302 permanent redirect) flag at the end of rewrite directive parameters (see documentation).
Rewriting URLs to individual pages
This task is not as difficult as it seems. For redirecting /wiki/index.php?title=Widget to /wiki/Widget, /wiki/index.php?title=Blue to /wiki/Blue etc. we can make use of map directive:
map $request_uri $uri_suffix {
~^/wiki/index\.php\?title=([^&]*) $1;
}
server {
...
location = /wiki/index.php {
rewrite .* /wiki/$uri_suffix?;
}
...
}
Please note that map directive must be declared outside your server configuration block!

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