Page 1 of 1

Need if statement to check it 'www' exists in URL...

Posted: Tue Jan 06, 2009 10:10 am
by Mike02GT
On my website I use google maps API which will not work properly unless 'www' exists in the url. Some search engines index my site w/o www in the url. The website is http://www.NewYorkMustangs.com

This is what I have so far, which find the url, but doesnt let me input anything before /cururl.php, as you can see here: http://www.newyorkmustangs.com/cururl.php

<?php
$homepage = "/cururl.php";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo "works";
}
?>

If i enter the entire URL as $homepage, it will not work. I think request_uri assumes everything before /cururl.php.

I basically need something to check if 'www' exists in the url, and if not, it will redirect to a different page.

Thank you very much!

Re: Need if statement to check it 'www' exists in URL...

Posted: Tue Jan 06, 2009 10:29 am
by volomike
Is this a local www redirection or something going across CURL? I don't get it.

If local, and if you have Apache, then here's something to add to your .htaccess file if you have mod_rewrite installed.

Code: Select all

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
 

Re: Need if statement to check it 'www' exists in URL...

Posted: Tue Jan 06, 2009 10:32 am
by Mike02GT
Sorry I was a bit confusing...

Basically I need a redirect code if the url = http://newyorkmustangs.com/page.php then it will redirect to http://www.newyorkmustangs.com/page.php

Re: Need if statement to check it 'www' exists in URL...

Posted: Tue Jan 06, 2009 11:29 am
by volomike
Are you on Apache? Are you on IIS on Windows? This is important for my solution below only works on Apache and was tested on Linux. If you're using something else, and if it supposedly supports .htaccess files, then you're good for go. If not, then you'll want to do the following in PHP by parsing the URL and doing a header() redirect.

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
 
To use, find out if you don't already have a hidden file in the root of your web directory called .htaccess. If you do, then tack this on at the top. If the file already has a "RewriteEngine On" inside, then remove the second one. If you don't have this file, then create the file .htaccess in the root of this web directory. So, if you have /var/www/mysite.com, or ~/www/mysite.com, for instance, the .htaccess file would go in there.

Re: Need if statement to check it 'www' exists in URL...

Posted: Tue Jan 06, 2009 11:42 am
by Mike02GT
Yes I am on Apache. I appreciate the help, I will try this after work.