Forum Moderators: phranque
RewriteCond %{HTTP_HOST}!^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Of course I used my real web site name. These 2 lines crash our Apache server, I don't know why. Do you guys know why?
Next, I tried another longer picee of code that PlanetOcean suggested to use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
</IfModule>
The above code is supposed to coreect ALL URLS on your site that arrive without the WWW.
This longer 8 lines of code does not crash our server, but it also does not work when we check it with example.com vs. www.example.com
The header checker tool does not show it producing the 301 code prior to the 200 code as it should.
Just trying to avoid Google's duplicate content penalties for www vs non www links to
your site.
[edited by: jdMorgan at 3:29 pm (utc) on June 15, 2006]
[edit reason] Example.com [/edit]
1) Did you restore the missing space between "}" and "!" in all lines where "!" occurs? (Posting on WebmasterWorld deletes this space, and it will cause an error if missing.)
2) What do you see in your server error log when this error occurs? (The diagnostic info in the server error log is often quite specific.)
3) Have you tried adding
Options +FollowSymLinks
above the RewriteEngine on directive? (This is required on many servers -- depending on the default serrver configuration.)
Just FYI, the first code snippet redirects any requests for resources in the non-canonical domain to the same resource in the canonical domain.
The second code snippet is specifically for redirecting requests for "index.html" to "/", and was first posted by a member here at WebmasterWorld.
Jim
Thanks for your tips.
Yes, we also tried these 4 lines of code also:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
And this one hangs the server, we get a status 500 Internal Server Error when we try to visit the home page in a browswer.
So this is wierd, the 4 lines of code above hangs the server, but the 8 lines of code we tried first, with the If statements does not hang the server, it just fails to do the rewrite.
I'll try adding the psace like you suggested and see what happens, then report back here.
Jeff
I just checked, on our .htaccess file, that space you mentioned IS IN THE FILE.
Like you said, this forum apparently removes the space. So rest assured we had the code in our file appearing as it should be.
Do you think Verio might have some setting turned off, that is preventing us from running these commands and pseudo environmental parameters?
Ok here is the code we used:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
</IfModule>
We made sure the space is in ther ebefore the!
Below is what we found in the error log, the server has a problem with our 'RewriteEngine' command, which is completley rediculous, this is a basic command that all Apache servers running 1.33 like ours should support! Look below, the excerpt from the rror log you'll see "Invalid command 'RewriteEngine'"
[Thu Jun 15 14:33:42 2006] [notice] caught SIGTERM, shutting down
[Thu Jun 15 14:33:43 2006] [notice] Apache/1.3.33 (Unix) configured -- resuming normal operations
[Thu Jun 15 14:33:43 2006] [notice] suEXEC mechanism enabled (wrapper: /usr/local/apache/bin/suexec)
[Thu Jun 15 14:33:43 2006] [notice] Accept mutex: flock (Default: flock)
[Thu Jun 15 14:33:52 2006] [alert] [client 70.152.54.44] /usr/local/apache/htdocs/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Thu Jun 15 14:34:18 2006] [alert] [client 199.246.2.11] /usr/local/apache/htdocs/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Thu Jun 15 14:34:28 2006] [alert] [client 67.35.113.171] /usr/local/apache/htdocs/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Thu Jun 15 14:34:49 2006] [notice] caught SIGTERM, shutting down
[Thu Jun 15 14:34:50 2006] [notice] Apache/1.3.33 (Unix) configured -- resuming normal operations
The <IfModule> stuff is only necessary of you plan to run this exact same code on multiple servers -- where one has the module loaded and the others may or may not. You can usually delete those <IfModule> containers, especially if the site's function depends on the code within; In that case, you wouldn't be able to run the site on a server without that module, so testing for it is redundant.
However, the "Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration" error message looks ominous... Do the rewrites in your httpd.conf file work or not?
If they don't work either, then contact your host and send them the error message. You may have a different setup than most I've seen from that hosting company.
Jim
# default virtual hosts
<VirtualHost 198.173.81.57:80>
SSLDisable
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteOptions inherit
</IfModule>
</VirtualHost>
That does not seem to cause any problems that we are aware of.
Also last night, I beleive we tried it without the if statements and it 500'ed the server.
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Please read the file [httpd.apache.org...] for more
# details about the DSO mechanism and run `httpsd -l' for the list of already
# built-in (statically linked and thus always available) modules in your httpd
# binary.
#
# Note: The order in which modules are loaded is important. Don't change
# the order below without expert advice.
#
# Example:
# LoadModule foo_module libexec/mod_foo.so
#
#LoadModule mmap_static_module libexec/mod_mmap_static.so
#LoadModule vhost_alias_module libexec/mod_vhost_alias.so
#LoadModule mime_magic_module libexec/mod_mime_magic.so
#LoadModule negotiation_module libexec/mod_negotiation.so
#LoadModule status_module libexec/mod_status.so
#LoadModule info_module libexec/mod_info.so
#LoadModule asis_module libexec/mod_asis.so
#LoadModule speling_module libexec/mod_speling.so
#LoadModule rewrite_module libexec/mod_rewrite.so
#LoadModule anon_auth_module libexec/mod_auth_anon.so
#LoadModule db_auth_module libexec/mod_auth_db.so
#LoadModule digest_module libexec/mod_digest.so
#LoadModule proxy_module libexec/libproxy.so
#LoadModule cern_meta_module libexec/mod_cern_meta.so
#LoadModule expires_module libexec/mod_expires.so
#LoadModule headers_module libexec/mod_headers.so
#LoadModule usertrack_module libexec/mod_usertrack.so
#LoadModule perl_module libexec/mod_perl.so
#LoadModule gzip_module libexec/mod_gzip.so
#LoadModule dav_module libexec/mod_dav.so
#LoadModule fastcgi_module libexec/mod_fastcgi.so
#LoadModule auth_mysql_module libexec/mod_auth_mysql.so
#LoadModule auth_pgsql_module libexec/mod_auth_pgsql.so
#LoadModule php4_module libexec/mod_php4-4.3.x.so
You need to uncomment
#LoadModule rewrite_module libexec/mod_rewrite.so
so that it reads
LoadModule rewrite_module libexec/mod_rewrite.so
If there's no AddModule, then it can't have been commented out.
Your hosting support should have been easily able to help you with this...
Jim
All we did was uncomment out the LoadModule rewrite_module libexec/mod_rewrite.so
in our config file, just like you said and that fixed it!
I even tested it with seoconsultants' headers tool which actually shows both status steps. First it shows the 301 response as it gets intercepted, then it shows the "200" response on the final www version of the it's supposed to land on. That's the way it's supposed to look.
I tested it with several pages on our site.
This is why we wanted the longer sportier code, because it will correct any page on your site to a www, instead of just the index page, which many people implement.
This is a better strategy to prevent duplicate content Google issues no all pages of your site, not jsut the home page.
I think I'm going to rename Verio as "Very Stupid I Know".
Jim, thanks so much for your help, you are a real lifesaver, gentleman, scholar, beer stud....!
This is what makes WebmasterWorld such a great site!
This case.... is CLOSED!
Jeff
Well, not quite. I don't like to divert threads into multiple streams, and prefer to fix one problem at a time. But if a user or a proxy appends a port number to the hostname in its request (a perfectly valid thing to do), your first rule will fail -- because you've end-anchored the hostname. You've also got some unnecessary, time-wasting anchoring in there, and a vulnerability to an infinite redirection loop if your site is ever visited by a true HTTP/1.0 client. I'd suggest:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#
[b]RewriteCond %{HTTP_HOST} .[/b]
RewriteCond %{HTTP_HOST} !^www\.example\.c[b]om[/b]
RewriteRule [b](.*)[/b] http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
Problem is we get errors for every canonical case we try. The server does not hang or naything, but the code simply does not work. If we do non-www version we get the error page that takes you to MSN's search engine.
IF we do the www version, we get an error. Here's the code, did I miss something?
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
</IfModule>
This means that one or both of the following are true:
1) You have not defined the non-www domains in your DNS Zone File A records.
2) The server is not configured to recognize these non-www domain names using ServerAlias [httpd.apache.org] or a separate ServerName section.
In order for this mod_rewrite approach to work, requests for all of the domain variants must be 'recognized' by DNS, sent to the correct server IP address (this is the main function of DNS), and then the server must recognize those requested domain names and 'deliver' them to the directory in which the mod_rewrite code resides (or to the correct vitual server section of httpd.conf if your code is in httpd.conf).
If you're getting the ever-so-helpful MSN search, then you're missing one or both of those configuration steps. I'd also suggest dumping MSN Explorer and turning off 'Show Friendly Error Messages' in the Internet Options on your development machine, so that any DNS errors are not 'hidden' by being redirected to MSN (or any other) search.
Jim
This one gets filed under dumb programmer error.
We forgot to replace Example.com with our web site in the server. That's what I hate about forums that don't let you post the URL, you have to keep making sure you honor the rules of the forum, but change the code appropriately when you actually load it onto your server!
It was a late night wathcing our Miami Heat beat Mark Cuban last night.
We tested the code and as before, it works perfectly, header cheacker tool shows 301 > 200 perfectly.
Thanks again Jim!
But try finding information you need on forums that do; Many of them are so full of soley-self-promotional member posts that the good stuff is diluted into obscurity. Plus the fact that you may click on a link there, and find yourself the recipient of a nice ActiveX 'gift' from an unscreened malicious Web site link.
Another point is that many members who thought they'd gotten away with something by successfully posting their URLs here have later had to pleadingly request removal of their own posted links, because WebmasterWorld out-ranked their own site on searches because of those links. Also, competitors checking your back-links might gain useful insider knowledge by reading your posts here... So it becomes a case of, "Be careful what you ask for, you just might get it."
We prefer to keep the signal-to-noise ratio high here at WebmasterWorld, and the rules are as they are due to the owner's many years of forum-admin experience. "WebmasterWorld is a place to discuss doing business, not a place to do business."
Jim