I was testing a web service in PHP and Python. The address of the web service was, let's say, http://my.domain.com/my/webservice. When I tested the web service in PHP using that URL everything worked fine. But, when I used the same location but in Python using SOAPpy I got an error.
Below is the code I used to communicate with the web service (Python):
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice', namespace)
server.myFunction()
The respond I got from the server:
HTTPError: <HTTPError 301 Moved Permanently>
I figure out that if I add a trailing slash to the web service location it works!
from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice/', namespace)
server.myFunction()
Why the lack of the trailing slash causes the error?
They're different URLs. http://my.domain.com/my/webservice implies a file webservice in the my folder. http://my.domain.com/my/webservice/ implies the default document inside the my/webservice folder.
Many webservers will automatically correct such URLs, but it is not required for them to do so.
Because the actual server URL is:
http://my.domain.com/my/webservice/
The PHP library must be following redirects by default.
The error is a 301 redirect meaning the you are being redirected to the URL with the slash on the end by the web server.
It seems that PHP will auto follow this redirect and thus not throw the error, whereas Python won't. You will need to do the following:
Try to Connect to the initial URL
Catch any 301 redirect and possibly 302 redirects as well
If there was a redirect then try to connect to that URL instead.
The new URL should be available in the response headers.
HTH.
[Disclaimer: This is a copy of my answer from here. I know some people don't like this kind of copying, but this explains why the slash is important.]
Imagine you serve a page
http://mydomain.com/bla
that contains
Read more...
On click, the user's browser would retrieve http://mydomain.com/more.html. Had you instead served
http://mydomain.com/bla/
(with the same content), the browser would retrieve http://mydomain.com/bla/more.html. To avoid this ambiguity, the redirection appends a slash if the URL points to a directory.
What a SOAP-URL looks like is up to the server, if a slash is necessary depends on the server and the SOAP implementation.
In your case, I assume that the target server is an apache server and the SOAP URL is actually a directory that contains your SOAP handling script.
When you access http://my.domain.com/my/webservice on the server, apache decides that the directory is properly addressed as http://my.domain.com/my/webservice/ and sends a 301 redirect.
SOAP uses a http POST, its up to the client to decide if the redirect should be followed or not, I assume that it just doesn't expect one.
Other implementations of SOAP, e.g. Apache Axis in Java have URLs that look like Servlets, e.g. http://domain.com/soap/webservice without slash, in this case the URL without slash is correct, there is no directory that exists anyway.
Axis fails on redirects as well, I think.
Related
We have a client that hosts their IIS web server on AWS. When navigating to a particular PHP web application on this server, it works when there is a slash on the end, but not when it is absent.
this works:
https://example.com.au/application/
However, if one were to enter this into the address bar:
https://example.com.au/application
it redirects to the equivalent http address with a slash on the end:
http://example.com.au/application/
http is disabled via the firewall, so the result is an error.
Here is the request details in Chrome debugger
So my question is, what does my client need to check to ensure this redirect does not occur? or that instead of redirecting to HTTP, it redirects to HTTPS?
Additional info:
This same issue does not seem to occur with .NET web applications. Eg 'https://example.com.au/dotnetapp' will not redirect to 'http://example.com.au/dotnetapp/'.
There are no rules configured in "URL rewrite"
IIS logs show requests when the HTTPS url is triggered, but not the HTTP one.
Edit: This seems to be due to browser caching. After disabling browser caching, i can see the 301 entry in the log files.
'index.php' is set as a default document
One possible reason is that the PHP project doesn't know that the secure connection is active and so it's redirecting the page to the http version when adding the slash.
PHP application can detect the secure connection by the $_SERVER['SERVER_PORT'], $_SERVER['REQUEST_SCHEME']. But if the application is behind some reverse proxy (e.g. Varnish or Amazon’s Elastic Load Balancer), the connection to the PHP application is probably not secured. PHP should be informed about the original secure connection with X-Forwarded-* headers.
Please check if the PHP has these variables set:
$_SERVER['HTTP_X_FORWARDED_PROTO']: should be https,
$_SERVER['HTTP_X_FORWARDED_PORT']: should be 443.
Symfony framework
If the application is using the framework, e.g. Symfony, it should be configured to trust the IP of the reverse proxy and to trust also these headers:
# config/packages/framework.yaml
framework:
# ...
# the IP address (or range) of your proxy
trusted_proxies: '192.0.0.1,10.0.0.0/8'
# trust *all* "X-Forwarded-*" headers
trusted_headers: ['x-forwarded-for', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-port']
# or, if your proxy instead uses the "Forwarded" header
trusted_headers: ['forwarded']
See https://symfony.com/doc/current/deployment/proxies.html for more details and https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly for more detaiils if the IP address of reverse proxy server changes.
Looks like you are setting location header in the 'index.php' file and so browser is redirecting to the http url.
If the index.php has code like below, replace the http to https and to the correct URL
header("location:http://example.com.au/application/");
Updated :
Also check your folder to see if any other files are redirecting.
Please make sure the index.php is listed as the first in the default document list and none of the other files contain redirect code.
You can search for "meta http-equiv="refresh" http tags in all the files in folder to see if they are redirecting.
I need to access http://server.com/folder and get a default index.php file with NO REDIRECT , which means I need Apache to deliver http://server.com/folder/index.php when http://server.com/folder is called with no redirect. How can this be done? I see many solutions but some do not seem to work or perhaps are incomplete while other cause a redirect. The client I want to use is Mopira for Scanning and does not respond to the redirect as I suspect others will not as well.
This is Apache2 running on a local Ubuntu server so i is not a server exposed to the Internet
Alternately if I can force http://server.com/filename to load http://server.com/filename.php with no redirects this would also work but redirects do not work!
By mistake, I did not add the suffix .php when I wrote a URL into the browser during a check on my website. The browser ignored it and simply returned the page as if I had added the .php suffix, anyway! Is this normal? Do all modern browsers see a URL like www.website.com/thispage and simply go to thispage.php or thispage.html, if they exist?
Requests go to the server.
Any basic URL in the address bar of a browser make an HTTP request to the server defined by the domain (or hostname). So, http://www.something.com/here/there.x sends a whole bunch of info (in the "HEADER" of the request) to the IP registered for the www subdomain of something.com. That server gets the request, which in the header includes information about your browser, IP, type of request, the full address requested, any submitted data from a form, etc.
Your server's http engine decides what to do with it
Apache, nginx, iis, are different types of HTTP servers who's whole purpose are to listen for such requests, and decide what to do about them.
Typically, this includes setting some degree of defaults, for instance:
match the domain to a directory
something.com: /var/www/site1
strange.com: /var/www/site2
default: /var/default
match the path (everything after the '/' following the domain) to a file
check for a matching .html file
check for a matching .php file
Check permissions and authentication
Execute the file according to config
send the php file to a php process, and reroute the output
simple grab and dump the contents of images, txt, and html files.
Send a response according to what was just executed.
This is DRASTICALLY simplified, and there are many many layers and specifics, but I'm trying to keep this as simple as the question.
You tell apache (or whatever) what to do.
Apache can be configured to return a styleized google search for cats, any time anyone requests `http://blah.something.com/[anything]/*.good. It can be configured to do just about anything you want it to, based on the url you send it. The browser only decides what is sent.
It is completely up to the web server, and it's configuration, as to how a URL is handled. Web servers like Apache, NGinx, Cherokee, Litespeed, etc all have different mechanisms for what happens when a URL request hits it, and then what it decides to do afterwards.
For instance, you could have your web server attempt to add .php and .htm and .html suffixes to the requested url, in order to try to be helpful. Or simply serve up the 404 page, or send the request to the home page - eg index.php.
I have a php website. There is a domain example http://www.mydomain.com/clubs.php I want to mast it to http://www.mydomain.com/groups.php and the rest of the address will remain the same. Is it possible.
Please guide me how to achive it..
You could create a new file named groups.php and include in it clubs.php.
Or you could use mod_rewrite via .htaccess file.
For including file use:
<?php include_once 'clubs.php'; ?>
For rewriting use and add it after line RewriteEngine On:
RewriteRule ^groups.php$ clubs.php [L]
Hope I've understood what you're talking about with mask.
If I understood you correctly, then you need to use apache directive URLRewrite (mod_rewrite). Google it. It's complex to explain here, but basicaly there are few lines to add in .htaccess file and your webserver must support it. Some do not supoprt for securty reasons. If the server is administered by you then you can easily modify this in VirtualHost directive of yout httpd.conf file.
maxim
Use the Apache mod_rewrite module, see below :
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://docforge.com/wiki/Clean_URL
You cannot "mask" a domain or URL. A link points to a certain URL. That URL is necessary for the browser to find the server it's supposed to send the request to and to make the request. The browser will send the request to that URL and it will display the URL it sent the request to in the address bar. You cannot make it send a request to one URL but have it display a different URL.
You can make your webserver react to that URL/request any way you want. Just because the request said "clubs.php" doesn't mean the webserver will execute a file called "clubs.php" or that it has to execute some PHP file at all. That can be customized in the webserver itself, typically through URL rewriting in Apache. That's all just internal to the webserver however, it configures how your webserver reacts to an incoming request for a certain URL; it does not "mask" the URL.
If you have links pointing to "clubs.php" but you want them to point to "groups.php" instead, you'll have to change your links. No way around it.
You could redirect all requests for "clubs.php" to "groups.php". I.e., when your webserver receives a request for "clubs.php", it responds to the client by telling it to ask for "groups.php" instead. That's a redirection. It will still make your links point to "clubs.php" though.
I want to create a web site with pure PHP. I want to hide the url parameters. I.e. I want to make my web site with clean urls. Is there is any way to do this with out using any framework? Is cURL helpful to do this?
See URL rewriting in PHP without .htaccess if you don't want to or can't use .htaccess, else refer to How to: URL rewriting in PHP?.
Just have a look on it...before you start your stuffs
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
First of all: It is not possible with PHP only (at least not the forms of URL I think of when reading clean URL). The web server needs to know how to handle requests and what requests are meant to be passed to your PHP script. Otherwise you will probably just get a 404 response.
Because the default behavior of a web server is to just take the requested URL path and try to map it to an existing file below the document root. If a corresponding file was found, either the file’s content is passed back to the client or – as in case of PHP files – the file’s content is passed to an appropriate interpreter and the returned data is passed back to the client. And if the file was not found, well, it responds with the status code 404. So at some point you need to configure your web server.
But after that, when the request was passed to your PHP script, you sure can use just PHP to establish clean URLs. And I would rather suggest to do that with PHP than with web server utilities. Because your PHP application should know best how to handle a requested URL.
In PHP, all required information are in the $_SERVER variable:
$_SERVER['REQUEST_URI'] holds the requested URL path and query (you can parse that with parse_url), and
$_SERVER['PATH_INFO'] holds the PATH_INFO if you’re using that (see Apache’s AcceptPathInfo directive).
Try to rewrite url using php and rewrite url using .HTACCESS.
For example, original url,
www.domain.com/item.php?product=Cars for sale in amazon
with php
www.domain.com/item.php?product=Cars-for-sale-in-amazon
and with .HTACCESS file
www.domain.com/Cars-for-sale-in-amazon
Nope, no curl or framework doing this. Nor php at all.
It is web server who deal with urls.
So, if you want fake urls, you have to set up your web server to redirect certain urls to certain scripts.
The most common way is to use Apache web server with mod_rewrite module
From what I have read and understand of it, there's 2 ways you can do this:
The first being mod_rerite where everything seems to re fone through rewrite rules through the .htaccess file fairly simple to do but can put big load on webserver with large sites
Use PHP to control the rerites this does use .htaccess but only to redirect everything back to the index.php where a dispatcher reroutes paths as necessary. There's a fantastic tutorial of this at phpvideotutorials.com the tutorial is called the tumblelog.