Nginx Redirect for WooCommerce Checkout Endpoint - nginx

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.

Related

Remove part url use nginx [duplicate]

I have a web server which accepts https://www.example.com/API/ooo/xxx, https://www.example.com/API/aaa/bbb, and https://www.example.com/API/xxx/yyy ... etc.
Now we want to redirect the above quests to https://www.example.com/ooo/xxx, https://www.example.com/aaa/bbb, and https://www.example.com/xxx/yyy ... etc.
I've tried using rewrite key word in nginx:
location /API/ {
rewrite ^/API(.*)$ https://$host$1 redirect;
}
This works for GET requests. But it turns POST requests to GET requests. This is something I don't want.
How can I preserve the http method while redirecting /API/* to /*?
This post says that I can use 307 redirect. But rewrite doesn't seem to support 307 redirect. And I can't figure out how to use $1 regular expression property in return.
Use a return statement. You can use a regular expression location block to capture the part of the URI to return.
For example:
location ~ ^/API(/.*)$ {
return 307 $1$is_args$args;
}
Note that with the regular expression location directive, its order within the configuration is significant, so you may need to move it. See this document for details.

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!

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 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 rewrite: add index.php

how do I do a rewrite in nginx from
http://www.example.com (with or without trailing slash)
to add index.php at the end?
For the example above the result should be
http://www.example.com/index.php
No special rules, just one domain to change.This should be simple but I can't seem to figure it out :-(
Thanks!
Assuming you mean you want the user's address bar to change, you just need one extra location in your server block:
location = / {
return 301 http://www.example.com/index.php;
}

Resources