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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mike02GT
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 10:04 am

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

Post 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!
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

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

Post 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]
 
Mike02GT
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 10:04 am

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

Post 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
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

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

Post 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.
Mike02GT
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 10:04 am

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

Post by Mike02GT »

Yes I am on Apache. I appreciate the help, I will try this after work.
Post Reply