tags:

views:

306

answers:

3

Hi. I have a simple html page that only uses PHP in two places

<?php preg_replace('/(www\.)?([^.]*)\.(com|info)/', '${2}', $_SERVER['HTTP_HOST']); ?>
<?php echo $_SERVER['HTTP_HOST']); ?>

In page is loaded on multiple domains, and I just want to display the host name as text in some other static content

I'd like to remove the need for PHP completely, but not repalce it with another full blown compiler or interpreter. I'd like to avoid using javascript. I can live without being able to do a regex to get the second level of the domain name, but would still like that option. Do I have any options for doing this via a simpler apache module than mod_php?

Theres nothing wrong with mod_php, I'm just seeing if I can minimalize the needs of this website I am working on.

A: 

I havn't tested it buy you might be able to use htaccess and do a rewrite like this:

RewriteRule (.*) $1?httm_host=%{HTTP_HOST} [L]

I don't know for sure that the %{HTTP_HOST} variable is available in a rewrite but it may work. You may need to use a condition to check for the ? in the URL.

Unkwntech
+4  A: 

I’d combine both mod_rewrite and SSI. Set an environment variable with mod_rewrite:

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.(com|info)$
RewriteRule ^ - [L,E=HOST:%2]

And then access that information in SSI with:

<!--#echo var="HOST" -->
Gumbo
That's nice. +1
Boldewyn
I wonder performance wise if mod_rewrite and mod_ssi is better than the mod_php version. I doubt this site will ever get enough traffic to matter, but its a good exercise in minimalism.
Justin Dearing
A: 

How about JavaScript? You only need two small changes on the site, that can easily be done. So you can plainly serve static HTML files. And to get the domain, you can use:

var domain = window.location.hostname;

Cheers,

Edit: To use mod_rewrite: You will have to set up two identical HTML files, with the correct domain in each. Then you can deliver them via

RewriteCond %{HTTP_HOST} .+\.com
RewriteRule index.html index.com.html [L]

RewriteCond %{HTTP_HOST} .+\.info
RewriteRule index.html index.info.html [L]
Boldewyn
Sorry, just read about not using JS. Still, it would be a viable way. Leaving the answer for consideration.
Boldewyn