Reputation: 8685
Consider the following scenario.
There is a site on a local network which can be accessed one of 2 ways:
Is it possible with php to only allow access using test.site.com?
EDIT:
While there were many good suggestions as to how to solve this problem, I went with Publi Design & John V.'s mod_rewrite suggestion, but I implemented it in the httpd.conf file and redirected to a forbidden page.
What worked for me was:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^test.site.com$ [NC]
RewriteRule .? - [F]
Upvotes: 0
Views: 59
Reputation: 1008
I would avoid the PHP request and go up one level. I haven't tried this, but maybe the .htaccess
file could help you, using something similar to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^11.11.11.111/testsite$ [NC]
RewriteRule ^(.*)$ test.site.com/$1 [L,R=301]
You might have to play around with it, but I would imagine something along those lines should get you to where you want to be.
Upvotes: 1
Reputation: 1756
As Michael P suggested in his comment, it's best to do this using a server configuration file, rather than with PHP. But it is possible with PHP, and rather easy. All you need is a simple if/else
statement using PHP's global variable $_SERVER
. Something like this should do.
<?php
// put this at the top of every page that will be accessed
if ($_SERVER['HTTP_HOST'] != 'test.test.com') { 'Please visit this site using the correct link: http://test.test.com'; exit; }
?>
Upvotes: 1