Forum Moderators: Robert Charlton & goodroi
And then there's the simple fact that there are other seach engines to think about. For me, having the technical set up on the server is still an important step - and it will still be so for a long time, I'd project.
What I don't know is if it was just a matter of Google seeing it as two pages (www and none) or if the page was hijacked. Can that still happen?
I think it's not likely that WMT is as good as proper 301 redirects in fixing this.
I am wondering about the difference between code I am using above and the code posted by Jim (in addition to the obvious difference in that I prefer to not use www)? The last line has a disparity involving: ^(.*)$ and the order in [L,R=301] which order I imagine does not matter, with ^(.*)$ looking more significant?
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
When using a 'catch-all' there is no need for the start and end anchor tag, so they are basically 'extra characters' in the file, which are absolutely unnecessary. .* will match 0 or more of anything, except the end of a line, so it matches every possible URL start to finish. The () store the match for back-reference. Since you will be checking this ruleset on every request it is a good idea to eliminate anything not absolutely necessary.
If you were matching a specific pattern anchoring would be important, not only to match correctly, but also to improve efficiency by eliminating some request from the comparison as soon as possible.
Another edit to the first ruleset was the addition of the \ preceding the ., which is necessary to match a literal dot rather than, 'any character except the end of a line', as a . matches. EG example.com will actually match exampleTcom, example1com, etc. It should not really matter here, except for correctness and form (which are always good to practice and have correct) since exampleTcom.com will not resolve to your domain.
It appears the capitalization was also corrected, the NC (no case) was removed (unnecessary due to the catch-all), and the L (Last) flag was added. The capitalization matters, although it is often translated correctly, but on some machines it can cause issues. The NC was an unnecessary flag, and the L flag is required unless you KNOW you do not need it. Failure to use a L flag on a ruleset can cause some very unexpected results, so unless you are doing quite a bit of rewriting and know when you should use or remove it, it is recommended to use it.
Personally, I like using a negative match, especially if you have wild-card subdomains on.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The above 'says' if the host ! (is not) www.example.com EXACTLY, and ! (is not) empty (the ? following the () pattern makes it optional - necessary for HTTP 1.0 clients), redirect any domain requested to www.example.com/The-URL-Requested
EDITED: I Was being silly early in the morning, so I had to remove a nonsensical line from my post. I'm done editing now. Really this time, sorry mods.
[edited by: TheMadScientist at 1:40 pm (utc) on May 8, 2008]
On Google Webmaster Tools my site is listed as "example.com" and my XML sitemap has the all the pages listed as "http://example.com" ; again without www
It's a growing niche and I have recently gotten more aggressive about developing backlinks and many of my new backlinks link to www.mysite.com and I'm worried about diluting my PR.
I'm a bit confused as to how to write my .htaccess; which of the following is what I want and if someone could be so kind as to explain why so that I might learn.
-----------------------------
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
or
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
[edited by: tedster at 6:00 pm (utc) on May 31, 2008]
[edit reason] switch to example.com - it can never be owned [/edit]
Think of RewriteCond as saying "if" and RewriteRule as "then". The first choice you gave says "if there is no www in the requested url, then add it." That goes in the opposite direction of what you want. So you would choose option #2.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
It was the only text in my .htaccess file and I got the following when trying to access my page:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, support@supportwebsite.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
--------------------------------------------------------------------------------
Apache/1.3.33 Server at ["example.com"] Port 80
PS Mods: this now may be more of a coding issue than a google search issue so if I need to move this to another forum let me know.
I am new to linux/apache and I've used the code on this trhead. It works fine on one script I am using ,
but on the other script, which is installed in a sub directory, it does not work on the pages in the sub directory,
There are 2 .htaccess, one at the root and one in the sub directory, initially I put the code just in the root directory .htaccess, it works fine for pages in the root directory , but the pages of the script in the sub folder just ignored it,
so I then placed the redirect script on the .htaccess of the sub folder, and still no joy
I am using
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
also tried
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com/subfolder/ [NC]
RewriteRule (.*) http://www.example.com/subfolder/$1 [R=301,L]
Would appreciate some help
Cheers
Further, it should be unnecessary to put the rule in both the root and the subfolder; Any code in root should be executed before Apache traverses to the subfolder. If this isn't working, add
RewriteOptions inherit
See the Apache mod_rewrite documentation for information on the values of server variables available to RewriteCond and RewriteRule.
Jim