Welcome to WebmasterWorld Guest from 23.20.219.0

Forum Moderators: Ocean10000 & incrediBILL & phranque

Move rewrite rules from htaccess to httpd.conf

Syntax works but nothing happening

     
9:19 pm on Apr 15, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Oct 24, 2003
posts: 578
votes: 4


I've decided to move parts of my enormous htaccess file to httpd.conf as I've heard it might improve performance. Using the includes editor of WHM under pre VirtualHost Include I am pasting my rules at bottom, saving and restarting apache. I read this article (http://httpd.apache.org/docs/current/howto/htaccess.html) and understand that certain simple changes need to be made to the rules such as:

In htaccess - RewriteRule ^photo/abc/$ http://example.com/photo/xyz/ [R=301,L]
In httpd.conf - RewriteRule ^/photo/abc/$ http://example.com/photo/xyz/ [R=301,L]

Neither way worked. I wasn't sure if I need to indicate the directory with:

<Directory "/home/myacount/public_html/">
my-rules-here
</Directory>
Did not make a difference. All the rules work fine in htaccess, what am I missing to make them go in httpd.conf?

[edited by: phranque at 10:05 pm (utc) on Apr 15, 2017]
[edit reason] example.com for clarity [/edit]

10:19 pm on Apr 15, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10809
votes: 53


in per-directory context, the Pattern is matched against only a partial path.
this applies to <Directory> containers and .htaccess files.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
1:38 am on Apr 16, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13599
votes: 412


In htaccess - RewriteRule ^photo/abc/$ http://example.com/photo/xyz/ [R=301,L]
In httpd.conf - RewriteRule ^/photo/abc/$ http://example.com/photo/xyz/ [R=301,L]
The leading / is only used if your RewriteRules are lying loose in the config file--and really, when would you ever do that? A more crucial difference is that RewriteRules in htaccess are typically located in the root folder, so ^ is equivalent to your domain root. If instead they are in the config file, then you need to either put them in a <Directory> section that corresponds precisely to the domain's root, or change all rules to show the appropriate physical filepath.

I've heard it might improve performance
Mmmwell, yes and no. If you get to the point where you've entirely eliminated htaccess for the whole domain, you will be able to turn off all Overrides settings. This leads to a slight gain in performance, because the server no longer has to go looking for htaccess files along the full path of every request. (The server has no memory. It does not know that it checked two milliseconds ago and found no htaccess. Besides, you might have added between one millisecond and the next; the server has no way of knowing.) Other than that, there's a tiny gain because in htaccess, Regular Expressions are recompiled from scratch every time, while in config they are compiled just once, at server startup. (The server doesn't remember much, but it can flawlessly memorize its own config file.)
3:23 pm on Apr 17, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Oct 24, 2003
posts: 578
votes: 4


Thanks for the answers...I must not have done something wrong with the directory structure, because I tried this both ways.

@lucy24, I've read that since the rules are complied once in httpd.conf when you restart apache that this is a major performance gain, not minor. All the rules have to be parsed for each request in htaccess. I have about 8,500 lines in my htaccess and it's over 1mb now so I am eager to see how much of a performance improvement there is. My server response time averages about .6-.9 secs now, very slow...that's double to triple what it used to be when the file was much smaller.