Forums / General Questions / php check domain name and then redirect

php check domain name and then redirect

Locked

Views: 7,737

Results 1 to 5 of 5
This thread is locked. New replies are disabled.
15 Aug 2008, 8:25 PM
#1
forced avatar

forced

New Zenner

Join Date:
Oct 2007
Posts:
61
Plugin Contributions:
0

php check domain name and then redirect

Here is my issue in a nutshell. I have 2 domain names:
https://www.aacorvette.com is the primary
https://www.aandacorvette.com is the secondary and setup as a domain alias

I have an SSL for https://www.aacorvette.com and only want to use that one.

Right now I have this to check and see if the URL is aacorvette or aandacorvette and redirect if incorrect.

<? 
$aa = 'www.aacorvette.com';
$url = $_SERVER['SERVER_NAME']; 
if ($url != $aa)
{
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://www.aacorvette.com');
}
?>

This seems to work OK, but if the user has http://aacorvette.com in the bar, it will forward them to the https://www.aacorvette.com address. (not that big of a deal)

I tried to find a funtion that would use a "contains string in variable" but I couldn't find a good way.

Is this the best way to do this? If so, is the best place to put the code in the tpl_main_page.php file?

Thanks!

Steve

15 Aug 2008, 8:36 PM
#2
forced avatar

forced

New Zenner

Join Date:
Oct 2007
Posts:
61
Plugin Contributions:
0

Re: php check domain name and then redirect

I forgot to add that I am on a windows box so I can't use .htaccess and I am on v1.3.8a.

15 Aug 2008, 10:59 PM
#3
skipwater avatar

skipwater

Totally Zenned

Join Date:
Jun 2008
Location:
Washington, DC
Posts:
775
Plugin Contributions:
1

Re: php check domain name and then redirect

Try this:

<? 
// Must be www.aacorvette.com or will redirect
if ( $_SERVER['SERVER_NAME'] != 'www.aacorvette.com')
{
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://www.aacorvette.com');
}
?>

Skip

• 446F63746F722057686F •

15 Aug 2008, 11:01 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: php check domain name and then redirect

forced:

I tried to find a funtion that would use a "contains string in variable" but I couldn't find a good way.strstr() is a "is string contained in string" function.

forced:

Is this the best way to do this? If so, is the best place to put the code in the tpl_main_page.php file?
I'd be inclined to put it in the master /index.php file in the root of your site.

But better yet, I'd recommend researching your windows webserver configuration for smarter alternatives which would give you control similar to .htaccess. Your hosting company should be able to offer some ideas.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

16 Aug 2008, 12:15 AM
#5
forced avatar

forced

New Zenner

Join Date:
Oct 2007
Posts:
61
Plugin Contributions:
0

Re: php check domain name and then redirect

Thanks for the help guys! I tried using the strstr() but couldnt figure out how to make it work. Like if I needed to say true or false, etc. I will look further into something like a .htaccess file. In the meantime I am going to try Skip's way and put in the index.php. I can't believe I did all that with the variables and it was that easy!

:wacko:

-Steve