API redirect with ProxyPassMatch? - regex

I am trying to do the following
setup a proxy so i can go to this url
http://localhost/shopify-api-myshopifystore
that points to https://myshopifystore.myshopify.com
this code below works BUT I want to be able to go to
http://localhost/shopify-api-myshopifystore/admin/api/2021-01/orders/4371107971258.json
that points to
https://myshopifystore.myshopify.com/admin/api/2021-01/orders/4371107971258.json
so anything after
/admin/api/2021-01/orders/4371107971258.json
can be a wildcard
and the below code doesnt do that my webpage just shows 404 Not Found
ProxyPassMatch "/shopify-api-$/" "https://$1.myshopify.com"
ProxyPassReverse "/shopify-api-$/" "https://$1.myshopify.com"

Related

Regex - redirect from website.com to website.com/us and append substring

Can anyone help me correct my regex?
My hosting provides IP geolocation and I want to redirect US clients to another sub-directory of my website.
The website runs on a Nginx server and the hosting provides an interface where one can add redirection rules. The interface consists of Domain, Redirect from, Redirect To, Traffic from (country) and HTTP status code 301 or 302.
Example:
for all non-US clients website.com/blog/article/really-good-book
only for US clients website.com/us/blog/article/really-good-book
I currently have:
Redirect from ^/(?!us/)(.*)$
Redirect to /us/$1
This currently redirects me to website.com/us/index.php and nothing else. So the redirect is applied, it only appends index.php instead of blog/article/really-good-book.
Thank you for your help.
Eventually I found my own solution:
Redirect from ^(?!(/us|/index.php|/wp-admin|/wp-login.php))(.*)$
Redirect to /us/$1
I also added a rule for my wordpress backend, otherwise it would always redirect me back to website.com/us while trying to access website.com/us/wp-admin.

How to write regex for apache ProxyPassMatch to reverse proxy API calls

I have an angular 4 web application which is hosted on apache 2.4. The application makes use of an API written in nodejs javascript running over express. Both the website and the API service are running on the same machine but on different ports. The website is on port 80 and the API service is listening on port 9000.
I would like to set up apache to do reverse proxy for all the API calls.
For example, any url that contains /api/ I want it rewritten by apache to point to the API url:port. If I use ProxyPass like the following lines, the redirect works fine:
ProxyPass "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
ProxyPassReverse "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
What I do not know how to do, is to use the ProxyPassMatch directive and create a regular expression so that any url that contains /api/ is redirected to http://localhost:9000/api/.....
I tried the following but it does not work:
ProxyPassMatch "^/api.*$" "http://localhost:9000/$1"
ProxyPassReverse "^/api.*$" "http://localhost:9000/$1"
Neither does the following:
ProxyPassMatch "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
ProxyPassReverse "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
Any help would be appreciated. My regex skills are lacking!
Note: obviously 'localhost' can be an IP address or a domain, I am using it in the example for simplicity.
Many thanks!
Edit: I corrected the first example to use .* instead of just * as per Alex's comment.
I solved the problem. The correct way to do reverse proxy with apache on the above example is the following:
ProxyPassMatch "/api(.*)" "http://localhost:9000/api$1"
ProxyPassReverse "/api(.*)" "http://localhost:9000/api$1"
I knew the multiple regex examples I was trying were correct, as I was testing them with https://regex101.com/, but I was hard coding the second part of to a particular route in order to eliminate the issue of the second part being incorrect, but for some reason it does not like that. Once I understood that the (.*) part of the regex is the first capture group and used it as $1 in the second part, it all worked.
I hope I clarified the answer enough and it is useful to someone else.

How to rewrite subdirectory name in iis

My website is located at:
example.com/NAME/home.aspx?id=1
I want to make it so this url works without redirecting to original:
example.com/DIFFERENTNAME/home.aspx?id=1
Also, when hitting the first url, I want it to be redirected to the 2nd.
I have url rewrite module Tried modifying several inbound rules, but cannot make it to work.
You can use HTTP Redirect and reference link follows
https://www.iis.net/configreference/system.webserver/httpredirect

How to redirect traffic from one domain to another with Wordpress Redirection Plugin

I'm using the Wordpress Redirection Plugin on my site, and need to resolve a duplicate content issue by adding a 301 redirect from:
www.mysite.com
to:
www.mysite.org
NB: Both URLs share the same IP address, and the same content.
The redirect is currently working correctly for the root domain, but I also need to use a regular expression to ensure that all internal pages are also redirected to the same path on the new domain:
www.mysite.com/a-random-page/ --> www.mysite.org/a-random-page/
Any pointers appreciated.
Try using Regular Expression:
Source URL: http://www.example.com/(.*)
Target URL: http://www.example.org/$1
Check the Regular Expression Box.
This will redirect www.example.com/a-random-page/ to www.example.org/a-random-page/.

Running the django admin over https using apache2

I have a django web application that's running on apache 2.2.14 and I want to run the admin application over https.
Having read considerable discussions on using a proxy, writing middleware, running alternative wsgi scripts, the chaps in #httpd came to my rescue. The solution is so simple, I was surprised I didn't find it online, so I'm curious to see if I've made some glaring assumptions or errors.
One complication was that I also wanted to run one of my django apps in the site over https, that is everything on /checkout.
Essentially, if a user requests a URI starting with /admin or /checkout on http, they are to be redirected to that URI but on https. Conversely, if a user requests a URI that does not start with /admin or /checkout on https, they are to be redirected to that URI but on http.
The key to solving this problem was to use Redirect and RedirectMatch directives in my VirtualHost configuration.
<VirtualHost *:80>
... host config stuff ...
Redirect /admin https://www.mywebsite.com/admin
Redirect /checkout https://www.mywebsite.com/checkout
</VirtualHost>
<VirtualHost *:443>
... ssl host config stuff ...
RedirectMatch ^(/(?!admin|checkout).*) http://www.mywebsite.com$1
</VirtualHost>
Another approach is to use #secure_required decorator. This will automatically rewrite the requested url and redirect to https://... version of the URL. Then you don't have to have Redirect in *:80 configuration. *:443 configuration may still be required for performance purpose if you want other traffic to go through normal http traffic.
I tried your solution, but ran into several problems. First, the formatting on the admin site disappeared, as if it could not find the admin static files. Second, if I tried to reach the non-admin site through https, the browser would not find it and redirect me to Yahoo search. Oddly, if I edited the yahoo search URL to eliminate all text except my correct URL (minus the http://), it would continue to search through yahoo for my site. However, typing the exact same URL afresh sent me to my site.
I solved all of these issues by simply removing the
RedirectMatch ^(/(?!admin|checkout).*) http://www.mywebsite.com$1
directive.
I should mention that I don't have a /checkout section on my site and am only trying to secure /admin. ... and yes, I did substitute my URL for "mywebsite.com"
What you described should work, but there may be a problem in the future if you need to make changes to which paths are/are not HTTPS. Because this method requires the ability to correctly modify the Apache config file it means you do not want novices in the loop. Screw up the config file and your site can go 500-error in the blink of an eye.
We chose to have a simple text file that had a list of the must-be-HTTPS paths. Anyone on the project can edit it and it is checked for correctness when it is loaded. We handle any needed redirects to/from HTTPS in middleware and it seems to work just fine. This method will also work if you are running anything other than Apache.