Welcome to WebmasterWorld Guest from 54.166.220.138

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Total site 301 Redirect

     
10:04 am on Jun 11, 2012 (gmt 0)



Hi Guy

I moved My main site to a new domain 3 weeks ago, I am losing traffic and although i can see the 301 is working i cannot help but think there is something wrong.

I have done this before using .htaccess and saw changes within 36 hours, is it possible someone can cast an eye over these 2 files that are on the main server and let me know if all is ok

This particular site is looked after by a third party so all changes have to go through them and there is no way around this

File 1

DirectoryIndex redirectall.php
ErrorDocument 404 /redirectall.php


File 2

<?php
# redirect everything from site1 to www.site2.co.uk

$req=$_SERVER['REQUEST_URI'];
$correcturl="http://www.site2".strtolower($req);
#die($correcturl);
#header("Location: $correcturl",true,301);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correcturl");

?>


In Google doing a site:www.mysite.co.uk both sites are still indexed with only the home page successfully moved .

Is this normal or am i being a little inpatient

Any feed back would be much appreciated
10:18 am on Jun 11, 2012 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



redirectall.php

what does this script do?

$correcturl="http://www.site2".strtolower($req); 

.co.uk?

are you getting the correct Location: header value?
you should use Live HTTP Headers for firefox or a similar tool to check this.

if you can look at your server access log i would verify that googlebot is crawling site1 and getting the proper 301 responses.
it's possible that google lost interest in crawling inner pages of site1 once the home page was moved and it isn't seeing any other links to the deeper content.
10:21 am on Jun 11, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The
.co.uk
is missing from your example code.

By using the
ErrorDocument
directive only files which don't exist on the old site will generate a redirect. If you haven't deleted all the files the redirect will not happen. Are you sure that the redirect is being correctly generated, and that there's no 404 header in there? Use the Live HTTP Headers extension for Firefox to check it out.

I would not use the ErrorDocument to generate a redirect.
Instead, I would have used:
RewriteCond %{REQUEST_URI} !redirect\.php
RewriteRule .* /redirect.php [L]

in the htaccess file on the old site and then similar code to what you already have in the existing PHP file.

All of the above assumes that the old and new sites are on separate servers or at least in separate folders.



The usual way to do this is to point both domains at the same server hosting space and then just do this in hteccess:
RewriteCond %{HTTP_HOST} !(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


However, you have the added complexity that you need to convert some URLs to lower case. For that situation, this would work:

# Redirect mixed-case requests for any domain to new domain via redirect script
RewriteRule [A-Z] /redirect.php [L]


# Redirect all other requests for any non-canonical domain to new domain
RewriteCond %{HTTP_HOST} !(www\.example\.com)?$
RewriteCond %{REQUEST_URI} !redirect\.php
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


The first rule uses a PHP redirect script to redirect mixed case URLs. The second ruleset uses a standard 301 redirect to redirect the rest of the URLs.

This is one of the rare occasions where a rewrite is listed before a redirect. This is necessary otherwise mixed-case URL requests for any non-canonical hostname would generate a double redirect as an unwanted redirect chain.

The last RewriteCond is VITAL to prevent the redirect script itself being exposed as a URL back out on to the web when a non-canonical hostname is requested. Omit it at your peril.


Use example.com and example.co.uk in this forum. It deactivates the URL auto-linking function.
12:09 pm on Jun 11, 2012 (gmt 0)



Hi Guys

Thanks for the replys guys, you input is really appreciated

It was me i missed that off on this example but was on the original below is how it was

<?php
# redirect everything from example.com to www.example.co.uk

$req=$_SERVER['REQUEST_URI'];
$correcturl="http://www.example.co.uk".strtolower($req);
#die($correcturl);
#header("Location: $correcturl",true,301);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correcturl");

?>


My I.T guys is looking into this as we speak and we may just use the standard 301 htaccess redirect and not worry about the upper case URLs which was implemented over a year ago as there was a uppercase url hard coded into the blog which went undetected for a month or 2

The new site is on a different server

So i think ill keep it simple, and use the normal htaccess

Ill let you know how i get on but below is what i think is right

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.example.com/$1 [L,R=301]
12:34 pm on Jun 11, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



No. That code has several errors.

The index redirect MUST be listed first otherwise it never gets to be actioned (or it gets actioned on the new site creating an unwanted double redirect).

Requests for "main" will not be redirected because it is missing from the
RewriteCond
pattern.

html|htm
can be simplified to
html?
making the '
l
' optional.

The pattern for the canononical hostname
RewriteCond
should be as per the previous post ustilising ! and ( )? and www, and not just ^example\.com

Add a blank line after each
RewriteRule
to make the code easier to read.

Only index URLs without parameters are redirected with the index filename stripped. Index URLs with attached parameters are redirected including the index file name. Is that going to be a problem?

You can still implement the to-lower-case fuctionality. The rewrite to the PHP script that I detailed above should appear after the index redirect and before the canonical hostname redirect.
1:23 pm on Jun 11, 2012 (gmt 0)



Many thanks for the reply

I suppose the issue we are having is that although my web guy is very good at what he does and he is fine with PHP but htaccess coding is not his thing so i think he needs to understand a few commands and what exactly they do

Can you explain this line please and what exactly it does, i have been trawling the net and there are a few misleading articles hence the code i posted previous, i want to get this right so please accept my apologies in advance

What does this line mean

RewriteCond %{REQUEST_URI} !redirect\.php


So if i just post the below into a htaccess file it should do a site wide redirect along wit sorting out the canonical URL and upper and lower case


# Redirect mixed-case requests for any domain to new domain via redirect script
RewriteRule [A-Z] /redirect.php [L]

# Redirect all other requests for any non-canonical domain to new domain
RewriteCond %{HTTP_HOST} !(www\.example\.com)?$
RewriteCond %{REQUEST_URI} !redirect\.php
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
1:41 pm on Jun 11, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The
RewriteCond %{REQUEST_URI} !redirect\.php 
code is VITAL. It stops an external redirect to the script path happening for previously internally rewritten requests.

This prevents the redirect script path itself being exposed as a new URL back out on to the web when a non-canonical hostname is requested. That would otherwise result in an unwanted two-step redirection chain for non-canonical-hostname mixed-cased requests.

Fix the errors in the index redirect ruleset and add that before the other two rules. You'll then have a complete solution.
3:19 pm on Jun 11, 2012 (gmt 0)



many thanks for the responses

Changes made and updated, all seems to be working fine, will report back later if any changes.

all old files deleted and new htaccess file uploaded,

Thanks again
9:42 pm on Jun 11, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



RewriteCond %{HTTP_HOST} !(www\.example\.com)?$

Isn't there supposed to be an ^ anchor in there somewhere?
9:51 pm on Jun 11, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Err. Yes. Damn typos.

It should be:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$


However, when the site is hosted on www and it's the start anchor that is missing, the rule would only fail for URLs with something extra before the www - which is unlikely.

For a site hosted on non-www, omitting any of the start or end anchors would cause the code to fail.

Omitting the end anchor would be a disaster in any and all scenarios.
3:50 pm on Jun 12, 2012 (gmt 0)



Why not something a bit more direct?

RewriteMap lc int:tolower
RewriteRule . http://www.newdomain.com${lc:%{REQUEST_URI}} [R=301,NE,L]

You can place that in an
.htaccess
file in the old site's document root or the site's
VirtualHost
block (or the main server config).

Resources: RewriteMap documentation [httpd.apache.org].

--
Ryan
 

Featured Threads

Hot Threads This Week

Hot Threads This Month