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!
Need if statement to check it 'www' exists in URL...
Moderator: General Moderators
- 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...
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.
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...
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
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
- 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...
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.
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.
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Re: Need if statement to check it 'www' exists in URL...
Yes I am on Apache. I appreciate the help, I will try this after work.