简体   繁体   中英

PHP : IF Domain equals EXACTLY [x] then [actions] but not subdirectories/files

I am trying to execute code with PHP but only IF the URL is EXACTLY at the entry point of the website: http://mywebsite.com . So specifically ONLY on that URL, nothing after. I am stumped after trying multiple PHP IF ELSE statements to try gaining it, very close I feel.

<?php $host = $_SERVER['HTTP_HOST']; if($host == "www.mywebsite.com" or $host == "mywebsite.com") { ?> MYHTML1SHOWS <?php } else { ?> MYHTML2SHOWS <?php } ?>

This has given me success in appearing on the domain when most visitors will come to mywebsite.com, but continues to work on all subsequent sub files/pages/directories. Which is 100% not wanted.

So I thought of a work around like ELSEIF's to show MYHTML2 to target all my pages, as they are handily all within country allocated directories: /au/ , /asia/ , /nz/ , /uk/ etc.

<?php } elseif (stripos($_SERVER['REQUEST_URI'],'/au/') !== false) { ?> HTML3 <?php } ?>

This didn't work, but it was worth a try (works on its own IF statement in previous websites I've done, but I figure its clashing with the first IF statement which is more prioritized in the PHP). Appreciate any help guys, this has me stumped but would be ever useful. There were no similar questions on the net for only showing code this way.

(Background: I am implementing a 'Country Selector' that shows only on the entry point of mywebsite.com. I have already set up each country within their own sub-directories, thus no purpose of showing the country selector for them if a customer goes directly to one of those addresses).

You're probably doing some magic with Apache's mod_rewrite ... if not, that might be a good place to start looking. It sounds like the problem you're trying to solve is best done via Apache, either in your httpd.conf or (if enabled) via .htaccess.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Otherwise, the $_SERVER variables $_SERVER['PHP_SELF'] and $_SERVER['REQUEST_URI'] are probably of use to you.

http://www.php.net/manual/en/reserved.variables.server.php

$_SERVER['HTTP_HOST'] will only check the domain name, not the requested path. This is as you describe, but doesn't seem to be what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM