301 Redirect With PHP http:// to http://www.

Zero duplicate support forum.
Personalized HTTP 301 dynamic redirections.

Moderator: Moderators

301 Redirect With PHP http:// to http://www.

Postby jshpik1 » Wed Aug 15, 2007 12:47 pm

Hello,

I'm trying to do a 301 redirect all non www. to all www.domainname.com pages, should probably be included in the page_header.php file... Here's what I have:

# <?php
# if(!stristr($_SERVER["HTTP_HOST"], 'www')){
# header("HTTP/1.1 301 Moved Permanently");
# header("Location: http://www.domainname.com/" . $_SERVER["REQUEST_URI"]);
# exit();
# }
# ?>


The only problem is redirects all pages it's on as 301's to the listed domain. How do I make this so it redirects the page it's on if it's http:// to http://www. using 301? The problem is my hosting provider does not allow me to do this with htaccess. This might also be something that you may want to include in a future version of this :lol:
jshpik1
 
Posts: 6
Joined: Fri Aug 10, 2007 1:48 pm

Advertisement

Postby dcz » Thu Aug 16, 2007 8:30 am

The phpBB3 zero duplicate is actually checking the full URL, including the domain, so it's making sure about the prefix already, and does not even require the mod rewrite to work.

So with phpBB2, this code should work:

Code: Select all
if(strpos($_SERVER['HTTP_HOST'], 'www.') === FALSE){
     header("HTTP/1.1 301 Moved Permanently", FALSE, 301);
     header("Location: http://www.domainname.com/" . ltrim($_SERVER['REQUEST_URI'], '/') );
     exit;
}


++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 21219
Joined: Fri Apr 28, 2006 9:03 pm

Postby jshpik1 » Thu Aug 16, 2007 10:39 am

That's wierd then because things like http://www.domainname.com/viewtopic.php?t=36 redirect as a 301 to the mod rewrite version properly, but things like http://domainname.com/viewtopic.php?t=36 redirects with a 302 to http://www.domainname.com/viewtopic.php?t=36 which then directs with a 301 to the mod rewrite link.

If I place that code in page_header.php it redirects all pages with a 301 to http://www.domainname.com/ (including http://www.domainname.com/) which would get me removed from search engines in a hurry, and it also causes an infinite loop because http://www.domainname.com/ keeps redirecting to http://www.domainname.com/.

Thanks.
jshpik1
 
Posts: 6
Joined: Fri Aug 10, 2007 1:48 pm

Postby SeO » Thu Aug 16, 2007 11:02 am

jshpik1 wrote:the mod rewrite link


I thought you could not use .htaccess ?
SeO
Admin
Admin
 
Posts: 6333
Joined: Wed Mar 15, 2006 9:41 pm

Postby jshpik1 » Fri Aug 17, 2007 4:15 am

I can't do redirects with htaccess like the commands:

# Redirect
# RedirectMatch
# RedirectPermanent
# RedirectTemporary


So I'm assuming I can't do things like:

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


I've tried it and it doesn't work. I think the hosting provider has http:// automatically redirect to the www. version with 302. I can do mod rewrites though.
jshpik1
 
Posts: 6
Joined: Fri Aug 10, 2007 1:48 pm

Postby dcz » Sun Aug 19, 2007 12:22 pm

jshpik1 wrote:That's weird then because things like http://www.domainname.com/viewtopic.php?t=36 redirect as a 301 to the mod rewrite version properly, but things like http://domainname.com/viewtopic.php?t=36 redirects with a 302 to http://www.domainname.com/viewtopic.php?t=36 which then directs with a 301 to the mod rewrite link.


Did you install the zero duplicate mod ?

I cannot really get how you could have rewritten links if you cannot use mod_rewrite.

About the 302 header, you could try :
Code: Select all
if(strpos($_SERVER['HTTP_HOST'], 'www.') === FALSE){
     header("HTTP/1.1 301 Moved Permanently", TRUE, 301);
     header("Location: http://www.domainname.com/" . ltrim($_SERVER['REQUEST_URI'], '/') );
     exit;
}


++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 21219
Joined: Fri Apr 28, 2006 9:03 pm


Return to Zero duplicate phpBB2

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 2 guests