Forum Moderators: phranque

Message Too Old, No Replies

Regular .htaccess file giving me an Internal Server Error

         

Jeremy_H

3:55 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



I've just switched from shared hosting to a virtual dedicated server from the same company.

Before, all my files worked great, including my .htaccess file, but now, when I try to upload it, the site won't render, and instead gives me at: 500 Internal Server Error.

As soon as I delete the file, the page is fine, and as soon as I upload the file, the site goes down.

Here's what my file says:


AddDefaultCharset utf-8

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_REFERER} !http://[^/]*\example\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} [^/]*\.(gif¦jpg¦jpeg¦dgw)$ [NC]
RewriteCond %{REQUEST_URI} !/img/_null\.gif
RewriteRule ^(.*) /img/_null.gif [L]

RewriteCond %{HTTP_REFERER} !http://[^/]*\example\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} [^/](.*\.pdf)$ [NC]
RewriteRule ^(.*) / [L,R]
#RewriteRule ^(.*) /?doc=%1

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

Once I relalized that it was this file that was causing the problems, I tried to find the specific lines to see if there was an error in them.

I've learned that if it has these lines of text it causes the problem, but if they are removed, then it works just fine:


RewriteCond %{HTTP_REFERER} !http://[^/]*\example\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} [^/]*\.(gif¦jpg¦jpeg¦dgw)$ [NC]
RewriteCond %{REQUEST_URI} !/img/_null\.gif
RewriteRule ^(.*) /img/_null.gif [L]

RewriteCond %{HTTP_REFERER} !http://[^/]*\example\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} [^/](.*\.pdf)$ [NC]
RewriteRule ^(.*) / [L,R]
#RewriteRule ^(.*) /?doc=%1

Any ideas? This is getting frustrating ! :(

[edited by: jdMorgan at 4:47 pm (utc) on Oct. 2, 2005]
[edit reason] Example.com [/edit]

marcs

4:30 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Are you sure the new server has mod_rewrite enabled/installed?

jdMorgan

4:34 pm on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your server error log. It will often tell you exactly what the problem is.

In this case, the most common problem is that you need to add:


Options +FollowSymLinks

ahead of your RewriteEngine on directive.

The server error log will tell you if this is the case.

Jim

Jeremy_H

4:35 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Macus, I'm very new to the world of .htaccess, so I'm not exactly positive of that answer, or how I could test it.

By saying that when those two sections of code are removed, and that the www redirect section, when left it, works, does that mean that it has been enabled?

Jim, I'll head to my logs and check it out.

Thanks

Jeremy_H

4:49 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Nothing happened when I tried placing Options +FollowSymLinks above RewriteEngine On.

But, it looks like the error logs hold a key:

I have a long list of errors saying:

RewriteCond: cannot compile regular expression '!http://[^/]*\\mydomainname\\.com'

Does this mean that my VDS doesnt have something installed that can compile regular expression? Is it an error in the code?

Any ideas? Thanks

jdMorgan

5:40 pm on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well that means you have syntax errors in the code.

For example "\mydomain" may be invalid because you are attempting to escape the character "m" by preceding it with a backslash. This is not only unnecessary, but may be invalid.

The code has several examples of this, and I can't tell you what the correct solution is, because I'm not sure what your goal is. But taking a guess, I'd say that


RewriteCond %{HTTP_REFERER} !http://[^/]*\example\.com [NC]

should be rewritten as

RewriteCond %{HTTP_REFERER} !^http://[^/]*example\.com [NC]

or

RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)?example\.com [NC]

where the goal is to match "example.com" or "<anysubdomain>.example.com"

This problem occurs in several of your rulesets.

The only reason I can think of that this would not fail on your old server is that the two servers have different regular-expressions libraries installed. One ignores the questionable symtax, while the other does not.

The other possibility is a cut-n-paste problem like we have here on this forum, where the required space between "}" and "!" has been deleted. I corrected that problem when I edited your post above to replace "mydomainname.com" with "example.com" as required by our Terms of Service, but perhaps your original code was missing the space, rather than it having been deleted by posting here.

Jim

Jeremy_H

6:37 pm on Oct 2, 2005 (gmt 0)

10+ Year Member



Thank you so much Jim, your answer was dead on!

It looks like on this new server, it's more exacting then my former one.

By changing...

!http://[^/]*\example to
!^http://[^/]*example

...it now works fine, allowing both the page to load with an error, and also does the redirecting as required by the code.

I didn't realize that there was an error in the first place. I always check my html and css over at W3's website, and my robots.txt at another one. Does anybody know of any .htaccess file checkers?

Again, thanks so much!