Redirect only home page with php

In order to consolidate back link credit, and avoid a ranking penalty for duplicate content, I would like to redirect requests for my landing page from the www to the non-www version.  I don't want to use .htaccess to redirect every page in the site, because there are scripts on my site which require the www prefix, so I only want to redirect the landing page.  

I would prefer not to change the suffix of my landing page to php.  I believe that .htaccess can be coded so that the Apache server treats all htm pages as php, but I don't know what unexpected effects that might have.

Here is the snippet which I would like to include in my landing page:

<?php if ($_SERVER['HTTP_HOST'] == 'www.mauitradewinds.com'){
header("Location: http://mauitradewinds.com".$_SERVER['REQUEST_URI']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

Please advise if, and how, this could work.
LVL 1
ddantesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InsoftserviceCommented:
You could even redirect it from your dns panel.
Please check with your hosting control panel for the same, no changes in code nor in your .htaccess
0
ddantesAuthor Commented:
Thank you.  Just to be clear, are you suggesting that the dns panel could be configured to redirect only the landing page, and not the other pages within the site?
0
InsoftserviceCommented:
yes. It means if client tries to hit  mauitradewinds.com it  will be redirected to www.mauitradewinds.com .
If you could paste the .htaccess file it would great to resolve your issue
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Ray PaseurCommented:
On my sites, I often add this to .htaccess
AddType application/x-httpd-php5 .html .htm .php

Open in new window

The effect of that directive is to cause HTML and HTM scripts to be parsed through the PHP parser.  If there is no PHP in the script, nothing happens, but if there is PHP, it gets executed as the page is being created.  You can use a PHP redirect if you have something like that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddantesAuthor Commented:
Thank you both for your comments.
insoftservice:  I'm still uncertain about this.  It seems that changing the dns "target" would affect the entire site, not just the landing page.   It's good if www.mauitradewinds.com is redirected, but I don't want www.mauitradewinds.com/RezEasy to be redirected, or any other subfolder, or deeper pages on the domain.  That is why I am hesitating.

Ray_Paseur:  This looks promising.  I was concerned about parsing all pages through the PHP parser.  Are there any drawbacks to doing that?
0
Ray PaseurCommented:
"Any" is a very big word, sort of like "never."  You could make the argument that unnecessary PHP parsing introduces server overhead.  In my experience, the overhead was undetectable and the advantages of having PHP available in all my scripts greatly outweighed any perceived disadvantages, so I used it.
0
ddantesAuthor Commented:
Fair enough!
0
ddantesAuthor Commented:
I implemented the change to .htaccess, and my browsers now ask me if I want to open or download the home page.  I changed .htaccess back to its previous version so that my site remains available.
0
Ray PaseurCommented:
That sounds odd.  Where is your site hosted?
0
ddantesAuthor Commented:
Browser message
The host is ulysium.net  which uses Apache software on a Unix platform.
Here is a screenshot of Firefox loading the landing page with that .htaccess in place.
0
ddantesAuthor Commented:
If it is useful, here is a link to the server's php info...
http://mauitradewinds.com/PHP-info.php
0
Ray PaseurCommented:
OK, this is an unfamiliar PHP version and Linux build.  Here's what I recommend.  Contact the server administrator or help desk.  Show them the line in .htaccess that I showed you.  Ask them what their equivalent statement should say to cause HTML files to be parsed through PHP.

You might also want to ask them to move your site to a server that does not have Suhosin.  That thing, while well-intentioned, will cause a catastrophe for you one day.  A clue as to its efficacy is the copyright date which is many years old.  It will do things like throw away request variables without notice -- the kind of thing that people who were ignorant of security and good programming practices would do to try to "foil" attackers.  Fortunately PHP has come a long way since then, and Suhosin is no longer needed at all.  It's all risk and no reward to have your scripts on a Suhosin-infected server!
0
ddantesAuthor Commented:
Very good.  Thank you for your input, and  I'll find out how that server wants to parse html through PHP.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.