How do I get the root url of the site?
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
If you're interested in the current script's scheme and host. Depending on web server configuration (reverse proxy, rewrites), what is "current" may not be what you see in the browser, but it is what PHP thinks it is dealing with.Otherwise, if you have a URL at hand you want the root of, parse_url(), as already suggested. e.g.
$parsedUrl = parse_url('http://localhost/some/folder/containing/something/here/or/there');
$root = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . '/';
If you're also interested in other URL components prior to the path (e.g. credentials), you could also use substr() on the full URL, with the start of the "path" as the stop position, e.g.$url = 'http://user:pass@localhost:80/some/folder/containing/something/here/or/there';
$parsedUrl = parse_url($url);
$root = substr($url, 0, strpos($url, $parsedUrl['path'], strlen($parsedUrl['scheme'] . '://'))) . '/';//gives 'http://user:pass@localhost:80/'
Note: The offset in strpos() is used to handle the case where the URL might already be the root. Get root url of the site in PHP
To return the folder of current php file, use this script.
$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
$dir .= $parts[$i] . "/";
}
echo $dir;
How do I get the site root URL?
I've solved this by adding a web.config setting in AppSettings ("SiteRootUrl"). Simple and effective, but yet another config setting to maintain.
How get root url path
ASPX: Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
Implemented with Javascript:
<script type="text/javascript">
var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
</script>
Javascript/jQuery get root url of website
Here's a function doing the same as Hawk's post above, only much, much shorter:
function getBaseUrl() {
var re = new RegExp(/^.*\//);
return re.exec(window.location.href);
}
Details here: Javascript: Get base URL or root URL Get root URL for a PHP website
After doing a var_dump
of $_SERVER
and messing around I found that I could generate the base URL with this solution, in case anyone stumbles upon this question. I'm sure there are other ways but this is what worked for me.
<?php
DEFINE('BASE_URL', HTTP_TYPE . "://" . HTTP_ROOT . substr(__DIR__, strlen($_SERVER[ 'DOCUMENT_ROOT' ])) . '/');
Django: How to get the root path of a site in template?
I think the proper way here is use the {% url %}
tag and I'm assuming that you have a root url in your url conf.
urls.py
url(r'^mah_root/$', 'someapp.views.mah_view', name='mah_view'),
Then in your template:<a href="{% url mah_view %}">Go back home</a>
How to get the current root URL on JSP
Don't know if it's the optimal solution, but I achieved what I needed assembling the url like this:
${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}
How to get the url of the root page in template in Wagtail?
The URL of the root page is /
. Just write <a href="/">
- no need to use a template tag.
(You might think that's cheating, but it's literally the definition of the root page.)
If you have your include(wagtail_urls)
line in urls.py rooted at a path other than /
, and don't want to hard-code that path in your template, you can use: {% url 'wagtail_serve' '' %}
(note the empty string after 'wagtail_serve'
).
Related Topics
Best Way to Get Hostname with PHP
Php/MySQL with Encoding Problems
How to Call Curl_Setopt with Curlopt_Httpheader Multiple Times to Set Multiple Headers
How to Route Get and Post for Same Pattern in Laravel
Make Woocommerce Checkout Phone Field Optional Based on Shipping Country
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Save CSV Files into MySQL Database
Yii Framework: Using Data from Related Active Record Models for Searching
PHP Require File from Top Directory
How to Pass an Array of Checked/Unchecked Checkbox Values to PHP Email Generator
Sort Array Based on the Datetime in PHP
Customizing My-Account Addresses Fields in Woocommerce 3
Converting Float Decimal to Fraction