Adrian Macneil
Adrian Macneil

Reputation: 13263

How can I access the hostname in an ExpressionEngine template

What's the easiest way to access the HTTP host name inside an ExpressionEngine template (without resorting to using PHP in the template).

Has a plugin already been created to do this, or should I use some sort of global variable?

Bonus points if there is a way to access other HTTP server variables inside an ExpressionEngine template as well.

Upvotes: 0

Views: 646

Answers (4)

Victor Gutierrez
Victor Gutierrez

Reputation: 680

You could use an addon for this called Server Variable http://devot-ee.com/add-ons/server-variable

{exp:server_var:server var="HTTP_HOST"}

Any of the other server variables are also available: http://php.net/manual/en/reserved.variables.server.php

Upvotes: 3

Natetronn
Natetronn

Reputation: 466

You could add the following code to your /system/expressionengine/config/config.php file

$assign_to_config['global_vars']['my_http_host'] = $_SERVER['HTTP_HOST'];

This could then be used in templates like so:

{my_http_host}

If you wanted the protocol ie. http if not https you could do this:

$assign_to_config['global_vars']['my_protocol'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';

This could then be used in template like so:

{my_protocol}

Here is a list of PHP Reserved Server Variables

Upvotes: 1

Steven Grant
Steven Grant

Reputation: 1325

How about just using {site_url} which is a standard global variable?

If you want to assign a config variable then you could do:

$assign_to_config['global_vars']['gv_hostname'] = $_SERVER['HTTP_HOST']; 

and then within your template you could call it via {gv_hostname} of course this wouldn't add the http:// before the hostname.

Upvotes: 3

Alex Glover
Alex Glover

Reputation: 351

You can set the hostname as a global variable and use that. You can set the global variable in your config file with php, and then you will have access to it in the template.

Or you can use {path='site_index'}

$assign_to_config['global_vars']['global_var_name'] = 'Global var value';

Upvotes: 9

Related Questions