Forum Moderators: phranque

Message Too Old, No Replies

500 error from htaccess when trying to redirect for canonical URLs

         

hibernate

11:26 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



In another thread it was discussed how to best handle requests for cannonical vs. non-cannonical URLs. The following code for htaccess
was suggested so that all requests for www.example.com get redirect to example.com. however it creates an error trying to access my site.

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

jdMorgan

11:30 pm on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More information about this error may be available in the server error log.

What is in your server error log, then?

Jim

hibernate

4:43 am on Jun 3, 2008 (gmt 0)

10+ Year Member



[Mon Jun 2 22:33:47 2008] [alert] [client IP ADDRESS DELETED FOR PUBLIC POSTING] /var/chroot/home/content/b/e/s/[example]/html/.htaccess: Invalid command '\xef\xbb\xbfOptions', perhaps mis-spelled or defined by a module not included in the server configuration

hibernate

5:41 am on Jun 3, 2008 (gmt 0)

10+ Year Member



I then tried the following code found from a thread on this forum a few years ago:

# For use in document-root .htaccess
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If request Host header is non-blank (HTTP/1.0 requests don't send this header and can't be redirected based on it)
rewritecond %{http_host} .
# And if requested domain is NOT the canonical domain
rewritecond %{http_host} !^www\.example\.com
# Redirect to requested page in canonical domain
rewriterule (.*) http://www.example.com/$1 [R=301,L]

I still get an error and can't load the site; this time with the following in the log:
Mon Jun 2 22:38:44 2008] [alert] [client IP address deleted for forum posting] /var/chroot/home/content/b/e/s/[example]/html/.htaccess: Invalid command '\xef\xbb\xbf#', perhaps mis-spelled or defined by a module not included in the server configuration

jdMorgan

5:28 pm on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Invalid command '\xef\xbb\xbfOptions'

Your .htaccess file is apparently corrupt, or at least invalid.

Use a plain-text editor, set to output US-ASCII format (or UTF-8 without the character-set header) to edit your .htaccess file. Use Windows NotePad, for example, not a fancy editor or word processor program.

Jim

hibernate

8:45 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



You were right. Even though I was using NotePad with Word Wrap off, when I created the file with a plain text editor I no longer get an error message. Related question follows, (I decided to go with the second option:)

Options +FollowSymLinks
RewriteEngine on
rewritecond %{http_host} .
rewritecond %{http_host} !^www\.example\.com
rewriterule (.*) http://www.example.com/$1 [R=301,L]

When I type "example.com" in my browser's address bar, it does not resolve to www.example.com. Why?

jdMorgan

11:24 pm on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you flush your browser cache before testing? If the non-www "page" is cached, the browser will not send a request to your server until that cache entry expires -- instead it will just show you the cached copy.

You should end-anchor the domain name in your RewriteCond, and I recommend that (in order to ensure portability) you DO NOT feel free to substitute lowercase variable names for uppercase. e.g. %{HTTP_HOST} is the variable name given in the documentation.

Jim

hibernate

3:58 am on Jun 5, 2008 (gmt 0)

10+ Year Member



Thanks; it was the browser cache. end anchor added!