Guzzle request failing depending on the network I am connected, - guzzle

I am using guzzle to make request to my subdomain api.domain.com.
On the same phone hotspot. When I am at home, my guzzle request needs to have www.api.domain.com but anywhere near work, it takes api.domain.com and failed with the www. I can't figure why, this is happening.
Here's my code. Sometimes it works like that without any issue.
$client = new GuzzleHttp\Client([
'base_uri' => 'https://api.domain.com',
'timeout' => 10
]);
In my controllers
$get = $this->client->request('GET','/dashboard', [
'timeout' => 0,
'headers'=>[
],
'form_params'=> ['param'=>$param]
]);
But when I change locations and it doesn't work. I need to modify it by adding the www
$client = new GuzzleHttp\Client([
'base_uri' => 'https://www.api.domain.com',
'timeout' => 10
]);
my controllers stay the same and it works
$get = $this->client->request('GET','/dashboard', [
'timeout' => 0,
'headers'=>[
],
'form_params'=> ['param'=>$param]
]);
I get a guzzle cUrl error 6
My .htaccess looks like this in my api. I am not that knowledgeable with htaccess so do correct me if I made a mistakes.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=Authorization:%1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.api.domain\.com [NC]
RewriteRule (.*) https://api.domain.com/$1 [L,R=301]

Disable verifying SSL
$this->client = new GuzzleClient(['defaults' => [
'verify' => false
]]);
Did you try it ?

At the end of the day, it was a issue in the server configuration that the company made.
Sorry for bothering you with this issue.

Related

.HTACCESS Redirection Problem: index.php Before URL Slug

I am trying to achieve a simple http to https redirect and www to non-www. The problem is that .htaccess puts "index.php" before the url slug, resulting in a server error. Here is what happens:
WRONG BEHAVIOUR:
http://example.com/url-slug -> https://example.com/index.php/url-slug
DESIRED BEHAVIOUR:
http://example.com/url-slug -> https://example.com/url-slug
Note: I want all queries to redirect to the index.php page in the main directory, unless the requested file exists on the server. I'd like to achieve this without changing the url in the browser, which causes the server to crash.
(Objective: www -> non-www & http -> https)
CURRENT .HTACCESS SETTINGS:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am I doing wrong?
Your http to https 301 redirect should be at the top before other internal rewrite rules
RewriteEngine On
#http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make sure to clear your browser cache before testing this change.

Codeigniter 3 routing error

I'm getting routing error on codeigniter 3.
Not Found
The requested URL /index.php was not found on this server. Apache/2.4.7 (Ubuntu) Server
What I did.
My .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Also set AllowOverride ALL
My controller Test.php
class Test extends CI_Controller {
public function index(){
print_r('test');
die();
}
}
My route: $route['test/(:any)'] = 'test/$1';
And try to reach this page by url:
http://localhost/codeigniter/test/index.php
http://localhost/codeigniter/test/
Try this .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
And if you are accessing the controller just type(assumed codeigniter as your project folder name)
http://localhost/codeigniter/test/
What's the reason for this $route['test/(:any)'] = 'test/$1';?
This nominate that there will be argument passed to this. it can be number or letter. Ex http://localhost/codeigniter/test/aaa or http://localhost/codeigniter/test/25
Look at this
Your problem is that your passing a uri(2), uri(2) is intended for the controller's method when your trying to use this route codeigniter/test/1 you should specify your method first so it goes like this $route['test/(any)'] = 'test/index/$i'; btw codeigniter is consider as your uri(0).

Zend Framework Route Chaining not work as expected

I'm using ZF 1.11 for my project.
This is my routing configuration:
public function _initRoutes() {
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(false);
$router = $frontController->getRouter(); // returns a rewrite router by default
$langRoute = new Zend_Controller_Router_Route(
':lang/', array(
'lang' => 'vn'
)
);
$manufactureRoute = new Zend_Controller_Router_Route_Regex(
'manufacture/(\d+)-(.+).html',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'filter'
),
array(
'1' => 'manufacture_id',
'2' => 'manufacture_name',
),
'manufacture/%d-%s.html'
);
$manufactureRoute = $langRoute->chain($manufactureRoute);
$router->addRoute('manufactureRoute', $manufactureRoute);
$frontController->setRouter($router);
}
When user access URLs like :
http://mydomain.vn/**vn**/manufacture/1-apple.html
everything works well as expected.
But the URLs like these below work well too.
http://mydomain.vn/**abc**/manufacture/1-apple.html
http://mydomain.vn/**def**/manufacture/1-apple.html
How can I make sure that user is accessing my correct URLs (with "vn"), all others are redirected to my custom 404 page.
I tried to redirect all the incorrect URLs to 404 page by setting rule in htaccess file but not success.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index\.php|images|css|js|txt|png|jpg|gif|flv|swf|resource|publics|uploads|robots)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
***RewriteCond %{REQUEST_URI} !^/vn/ [NC]
RewriteRule ^[^/]+/(.*)$ /vn/$1 [R=301,L,NC,QSA]***
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.vn [NC]
RewriteRule ^(.*)$ http://mydomain.vn/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.mydomain.vn/index.php/(.*) [NC]
RewriteRule ^(.*)$ http://mydomain.vn/$1 [L,R=301]
So, it seems like the requirements for your route are:
a fixed list of supported languages; and
the lang value is captured into a request parameter.
So, how about turning the lang route from a Zend_Controller_Router_Route route into Zend_Controller_Router_Route_Regex route with pattern something like:
(\(vn\))
The outer parens are for parameter identification, the standard regex delineation; the inner parens are escaped as part of an (at this moment, trivial) "or" condition.
Then when you add more languages - say 'en' or 'de' or whatever - you could modify the pattern to a less trivial "or" condition:
(\(vn|en|de\))
Not directly tested, just thinking out loud...

Sending POST data in form with CakePHP redirecting from www to non-www

I am using CakePHP and i have just added this code to app/webroot/.htaccess in order to redirect all http://mywebsite.com to www.mywebsite.com
<IfModule mod_rewrite.c>
RewriteEngine On
##########added lines
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
##########end of adding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Now i have big problem. The login form doesn't work anymore. I guess it is because it tries to send the POST info to http://mywebsite.com/login and then in .htaccess it is redirected to www.mywebsite.com/login but the POST data is missing.
How can i solve this?
In CakePHP i am using this redirect when the user success in his login:
$this->redirect('/posts');
**Update:
I have also tried it with this other redirection with no results:
$this->redirect(array('controller'=>'posts', 'action' => 'index'));
THanks.

htaccess https redirect only on specific Zend Frameworks controller/actions

I'm new to this community, but I've found it really useful time to time.
I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)
I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.
Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.
The way I was trying to do it is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
It seems to work when I go to /member controller
But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.
Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?
Thanks in advance.
Alessandro
Try these rules instead (replace appropriate lines):
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/(member|shop)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^/(member|shop)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
These rules a bit simple (HTTPS will be applied to ALL URLs in /member/ and /shop/ controllers (e.g. /member/login, /member/dashboard, /member/orders/15423/invoice etc)
Negate ! should be before ^ in RewriteCond directive -- if you want your own rules then replace RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*)) by RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
A method we use to redirect to https is to leave the default Zend Framework .htaccess settings and create an action helper to redirect to https when required. Our action helper code is:
<?php
class RequireSSL extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
if(empty($_SERVER['HTTPS']) && $config['billing']['requireSSL'])
{
$redirector = $this->getActionController()->getHelper('Redirector');
$redirector->goToUrlAndExit('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
}
}
Then when you are in a controller action that you need to be accessed via HTTPS you simply call this function at the beginning:
$this->_helper->requireSSL();
Also, here is another method for using action helpers that is a little more detailed if you need it:
http://juriansluiman.nl/en/article/110/in-control-of-https-for-action-controllers
Hope that helps!
I have discovered where the origin of the problem is, but I'd still need support to understand how to solve it.
I have tested on a local linux machine the htaccess and the result was the same... testing separately the two https condition statements (on and off) they work correctly redirecting basing on the given RewriteCond regex. When putting together only the redirect from http to https works.
Https to http redirect works only if the regex is not matched, else it redirects to http://www.mydomain.tld/index.php
So I finally tried to delete the last htaccess statement and it started to work correctly, but, obviously, it does not find the url, as it does not redirect to the index.php anymore.
It looks like after the correct https redirect the index.php one creates the problem. So I'm asking myself if there is a way to avoid this and make it work correctly.
As I wrote before, this seems to be a common problem of this htaccess, as its behaviour is the same on the test and on the production server (different linux flavours).
I put here the working code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=302]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain.
The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.
I paste here the working code for further reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^yoursite\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} \.(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]