I'm looking for a way to get a page address exactly how it is displayed in the address bar of the browser.
My site is basically a PHP script and my goal is to determine if the users use http://mysite.com or http://www.mysite.com or just mysite.com or www.mysite.com to access the site. I hope to use this information to redirect the link so it would be in a single format. This is required for some third party tools I'm using.
so what I'm askin is if it's possible to get the url of a site, in PHP exactly how the browser is requesting it.
You can tell the difference between www.mysite.com and mysite.com by looking at $_SERVER['HTTP_HOST'], however the browser will always automatically add http:// to a URL (and some browsers hide it from the users as unnecessary information) so there's no way to know what they actually typed in.
The first two lines of an HTTP request look like:
GET /index.php
Host: www.mysite.com
The first line specifies the local resource (usually a file in your web directory), and the second specifies what hostname the user entered to find your site (this is especially useful for servers running multiple virtual web hosts). PHP allows you to extract both of these:
$_SERVER['HTTP_HOST']
$_SERVER['PHP_SELF']
Technically, $_SERVER['PHP_SELF'] specifies the filepath of the current PHP script relative to the root directory of this web host, but that should, in practice, be the same as the resource listed on the first line of the HTTP request.
As the other responses have mentioned, there's no way to tell whether the user typed http:// or not.
You cannot determine whether or not the user typed http:// or left it off directly from PHP. PHP will only be able to tell the final domain name ($_SERVER['HTTP_HOST']). The other functionality is handled in the browser.
Related
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 bought a domain and I want to redirect it to a directory on another subdomain.
Exemple:
A user type www.firstweb.com in the URL bar and he has to be redirected to www.secondweb.com/directory/ but the URL shown has to be www.firstweb.com without iframe.
Other exemple:
www.firstweb.com/contact/ shows the content of www.secondweb.com/directory/contact.php but the URL shown has to be www.firstweb.com/contact/ even after the redirect.
Both domain and servers are hosted by the same company (OVH).
I don't know if it is understandable but I dont know how to figure it out.
Thanks a lot for your help.
J.ROX
You can do this by installing NGINX (http://nginx.org/) on the webserver. Then you can check from which base URL the user is coming from, and return the appropriate content. This is also possible with an Apache server using Virtual Domain Names.
Or you can setup your DNS correctly, this can be done by your domain provider.
I installed OpenScholar in my domain:
http://scholar.web
Basically, when someone register for an account, his site URL is http://scholar.web/user, and his content will be at http://scholar.web/user/contents
I've been searching a way so that the URL is converted into: http://user.scholar.web/content for displaying his content via virtual subdomain (htaccess maybe)
Anyone can provide some solutions or guidance?
thanks
First of all, you'll need to change the DNS entry on the domain so that *.scholar.web points to your site.
Afterwards, (If you're using Apache) change the Apache vhost to accept *.scholar.web requests.
You can then use htaccess and/or PHP to do what you like with the URL formatting.
I used to have a domain called something along the lines of "iscool.com".
This is the method I used so I could use pseudo subdomains like "ben.iscool.com" and it would display content accordingly.
I have some PHP pages that I need to force SSL for. I don't want to do it with mod_rewrite or anything like that, I want to keep all the logic in PHP. Right now I have code that looks like this:
if($_SERVER["HTTPS"] != "on") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
exit();
}
This does not work however because the server name is generic as I am hosting multiple sites with different domain names. So the SERVER_NAME is "www". The above code will redirect to https://www/index.php which is not valid.
I have tried print_r($_SERVER) to see the variables available to me but none of them give me the full URI request (http://example.com/index.php), and the next closest option I can see instead of SERVER_NAME is SERVER_ADDR which will also not help me, because going to https://127.0.0.1/index.php will go to the default apache site and not to the definition for "example.com" (not to mention that my SSL cert would no longer be valid).
Any suggestions?
If you can't get the host name from any $_SERVER variable, and you don't want to explicitly set it in a config or anything, the only other option I can think of (besides doing it using mod_rewrite, which you stated you don't want to do for whatever reason) is to serve the user a blank page that says, "Please wait while we redirect you to the secure site..." and then handle the redirect using JavaScript to parse the current URL and redirect to the HTTPS version.
In the end I had the guy who manages the Apache proxy server to add ProxyPreserveHost On to apache2.conf and this enabled forwarding of HTTP_HOST (and SERVER_NAME) properly to the backend Apache server.
Thank you everyone for your suggestions, I'm sorry the problem ended up being something stupid that wasn't even in the scope of my question. If the Apache server I was working on did not have a proxy in front of it, most of your suggestions would have proven completely accurate.
I suspect you want to use the $_SERVER['HTTP_HOST'] setting in place of SERVER_NAME.
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
If your site URL isn't in $_SERVER I'd guess that you'd have to register it somewhere as a global variable. Each site would then have to register it's own $SITE_URL variable or some such so that you could fill it in with that.
In my humble opinion you should avoid doing theses absolute url stuff in your application.
The right place to do it si behind your application, for several reasons:
your application could be used in the
same time, via the same apache
server, with different names
your application could be used behind
some proxy, rewriting the base
url. (But you can try to detect
it)
See for a more detailled explanation this link http://www.makina-corpus.org/blog/relativeabsolute-url-and-proxies
So absolute url is bad, should avoid... I would build instead a nice url managment, where the rules would be simple (all /admin or /conn url needs to be on SSL) and let the proxys/web servers/load balancers handle it nicely. But if you really want to make this redirection in your application the used ways are:
using a configuration file where the
https absolute url and the http
abolute url is defined and unique (fine if your site name is unique and your application not done for large usage)
detect absolute url from proxy string
in HTTP_X_FORWARDED_HOST and/or
HTTP_HOST (better)
But most admins will say that absolutes url are bad :-)
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.