Forum Moderators: phranque

Message Too Old, No Replies

Moving all .htaccess files to httpd.conf

         

Frank_Rizzo

9:28 am on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I changed from a .htaccess/.htpasswd protected area system to one based on php.

I used to have a lot of .htaccess files scattered around and thus the server had a bit of extra load due to having to override etc.

Now there are just two .htaccess files and I think it would be even better to have them in the httpd.conf file.

My question is: Can anything which is in the .htaccess file go in httpd.conf?

Here's the .htaccess from the root of the home dir:

###############################################
#
# .htaccess in /home/mysite/public_html
#

#block bad guys
<Files ~ "^.*$">
order allow,deny
allow from all
deny from nnn.nnn.nnn.nnn
</Files>

rewriteEngine on
rewriteCond %{HTTP_USER_AGENT} ^LinksManager [OR]
rewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{QUERY_STRING} ^(.*)gratishost
RewriteRule ^.*$ - [F,L]

# prevent access from santy webworm a-e
RewriteCond %{QUERY_STRING} ^(.*)highlight=\%2527 [OR]
RewriteCond %{QUERY_STRING} ^(.*)rush=\%65\%63\%68 [OR]
RewriteCond %{QUERY_STRING} ^(.*)rush=echo [OR]
RewriteCond %{QUERY_STRING} ^(.*)wget\%20
RewriteRule ^.*$ [127.0.0.1...] [R,L]

# prevent pre php 4.3.10 bug
RewriteCond %{HTTP_COOKIE}% s:(.*):\%22test1\%22\%3b
RewriteRule ^.*$ [127.0.0.1...] [R,L]

# prevent perl user agent (most often used by santy)
RewriteCond %{HTTP_USER_AGENT} ^lwp.* [NC]
RewriteRule ^.*$ [127.0.0.1...] [R,L]

RewriteCond %{HTTP_HOST} ^mysite\.co\.uk
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [R=permanent,L]

RewriteCond %{SCRIPT_FILENAME} ([^/]+)\.wmv$
RewriteRule ^.*$ http://www.mysite.co.uk/course1/course1.html [R=permanent,L]
######################################################

As you can see there is a fair mix of different things going on there.

Can they all go in the httpd.conf file?

How does it fit in?

I guess I just create

<directory "/home/mysite/public_html">

</directory>

and paste the file between the directory tags?

In order to stop apache for scanning for .htaccess files in every dir I then need to add:

<directory />
AllowOverride None
</directory>

Does that sound right?

[edited by: jdMorgan at 2:04 pm (utc) on Sep. 15, 2005]
[edit reason] Disabled smilies to clarify code. [/edit]

jdMorgan

2:17 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to modify the regular expressions used to match URL-paths as well. In httpd.conf, all URL-paths seen by RewriteRule will begin with "/". So, for example,

RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [R=permanent,L]

will need to be changed to

RewriteRule [b]^/([/b].*)$ http://www.mysite.co.uk/$1 [R=permanent,L]


Patterns of ".*" which are not back-referenced will not need to be changed, so

RewriteCond %{HTTP_USER_AGENT} ^lwp.* [NC]
RewriteRule ^.*$ http://127.0.0.1/ [R,L]

should be fine as-is, although I should point out that this is an unnecessary 302 redirect, and using

RewriteCond %{HTTP_USER_AGENT} ^lwp [NC]
RewriteRule ^.*$ - [F]

would be much more efficient. (This simply returns a 403 response, instead of trying to redirect the client. Since most malicious clients will ignore redirects, there's not much use trying to redirect them.)

Also, the pattern in


RewriteCond %{SCRIPT_FILENAME} ([^/]+)\.wmv$

looks suspect. Since it's not documented, I can't tell what the purpose is. However, the effect of this pattern would be to prevent the rule from being applied to wmv files unless they are in your root directory. I suspect that a pattern of "[^.]+\.wmv$" may have been intended, and if so, a pattern of "\.wmv$" would work just as well. This would allow you to move the pattern to RewriteRule and dispense with the RewriteCond entirely.

Jim

Frank_Rizzo

2:23 pm on Sep 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers Jim, I'll give it a try.

as for this line:

RewriteCond %{SCRIPT_FILENAME} ([^/]+)\.wmv$

I think that is the solution to a problem I had a year ago. It solves the problem of users directly linking to a movie file whereas they should be reading the html instructions and playing the movie from that page.

What it does is to redirect requests for the wmv to the html, however, if the wmv is called directly from the page then no redirect (else endless loop). Well it works anwyay.