Welcome to WebmasterWorld Guest from 54.158.14.224

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: 590
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:10859
votes: 67


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:13744
votes: 462


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: 590
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.
6:00 pm on Apr 29, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

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


I have had no resolution to this issue. My rules will still not process when I move them to Pre Virtual Host Include in httpd.conf. I have spoken to tech support at my hosting company about it and they say the rules should work as written:

<Directory "/home/myaccount/public_html/">
RewriteEngine On
RewriteRule ^(d/)?old-url-1/$ http://example.com/photo/xyz/? [R=301,L]
RewriteRule ^(photo/)?old-url-2/$ http://example.com/photo/xyz/ [R=301,L]
</Directory>

Any ideas why this might not be processing? I've tried everything I can think of...
7:26 pm on Apr 29, 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:13744
votes: 462


Try removing the ^ anchor in your rules. It's the first and simplest thing to test.

Also double-check that there are no RewriteRules further along the same path, for example in htaccess.

old-url-1/
I hope this doesn't mean that every single individual URL has its own RewriteRule. If so, you should be looking at a RewriteMap--an option that wasn't available* in htaccess, but can be used in config. Or, in the alternative, rewriting to a php script to handle massive redirects.

* To be hair-splittingly technical: a RewriteMap can be used in htaccess, but it can't be declared.
7:49 pm on Apr 29, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

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


@ lucy24 Yes, every single old page URL and old image URL from my ten year old site has it's own rule, except for batches of URLs that I could knock out with regex. My htaccess file is 1.3mb large now and still growing. It has taken me six months of constant work to catch all the 404 errors on the server and also from Search Console and add them. I thought I had it mostly taken care of before I went live, but then I realized after careful analysis that my images had tons of inbound links and so those old image URLS got redirected too.

After a period of time Google supposedly stops looking for the old URL, but in my experience that never happens. Google requests pages that I haven't had in literally a decade and which have been 301 redirected for years. I don't know what a RewriteMap is, but if it's not a nightmare to implement I will look into it. Will also try the changes you mentioned. Thanks!
8:03 pm on Apr 29, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

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


@lucy24 Removing the ^ worked, thanks for that tip! It looks like the RewriteMap is beyond my capability.

[edited by: phranque at 1:00 am (utc) on Apr 30, 2017]
[edit reason] see Charter [/edit]

10:23 pm on Apr 29, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

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


@lucy24 I was able to get htaccess down from 1.3mb to 150kb by moving most of the rules to httpd.conf. I was rather surprised to find on testing that it made no discernible difference in the speed of my site. We're talking many thousands of rules, so I am surprised to find that. I tested at gtmetrix and at webpagetest.org.
1:03 am on Apr 30, 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:10859
votes: 67


moving most of the rules to httpd.conf. I was rather surprised to find on testing that it made no discernible difference in the speed of my site.

any changes to http.conf would remain ineffective until the server is restarted.
1:08 am on Apr 30, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

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


Server was restarted, and all the rules were tested and work...no discernible speed bump from delivering the rules via htaccess that I can see...
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members