Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite confusion

.htaccess versus httpd.conf rules!

         

craig1972

6:06 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Hi, some questions for people who've used mod_rewrite for longer than I have:

1. Is there any tangible performance gain by putting rules inside httpd.conf file? Problem is that everytime my host installs a new Apache server (recompiles it, to upgrade for example), the Rewrite rules go away and I have to redo them for all my virtual hosts every time. Having .htaccess in the domain accounts is nicer from this perspective, but my reading on these forums suggests that it's a little inefficient? (I'm on Apache2.2.6).

2. More importantly, I have noticed that two similar rules don't work the same across httpd.conf and .htaccess. Let's say my site root is as follows:

/public_html/site/myscript

This php file "myscript" is seen from the web as follows:

http://mypublicsite.com/site/myscript?p=123

Where "p" is the page number. In order to do away with the "?" mark for SEO, I want it to look like this:

http://mypublicsite.com/site/myscript/page123

Now, I can do this two ways:

1. In the folder "site" I can put a .htaccess:


RewriteEngine On
RewriteRule ^myscript/page([0-9]+)$ myscript?p=$1 [L]

This works.

2. In the httpd.conf, I can put this in my site's VirtualHost:


RewriteEngine On
RewriteRule ^/site/myscript/page([0-9]+)$ /site/myscript?p=$1 [L]

But this doesn't work! Any ideas why this may not be working and what I can do? Thanks!

jdMorgan

7:18 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This php file "myscript" is seen from the web as follows:

[mypublicsite.com...]

Actually, that looks like part of the problem right there. Why is "site" visible in the URL, and why aren't you using [site.com...] ?

It may be that you've got a disagreement between the DocumentRoot path and the path in your httpd.conf RewriteRules. If so, your server error log should show the path that the server is trying (and failing) to access to serve a page, and any disagreement between that path and the one you expect will be a strong hint as to where things are going wrong.

Jim

craig1972

12:49 am on Feb 11, 2008 (gmt 0)

10+ Year Member



Thanks, but "site" is a folder I do need to have. It's not something as
generic as "site". These are categories on my site, so they have
meaningful names for site structure hierarchy. "Site" is only for
an illustration of my problem with this example.

Anyway, can't RewriteRule do this -- do rules always have to be from
the root folder on a domain?

jdMorgan

12:53 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See my comment above about the server error log.

Jim

wilderness

1:05 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Problem is that everytime my host installs a new Apache server (recompiles it, to upgrade for example), the Rewrite rules go away and I have to redo them for all my virtual hosts every time.

My apologies for the "right turn off-topic"!

This is a rather valuable learned lesson.

Don't rely on your host for back up copies of your files (wthether system or otherwise).

My own back ups to my local machine are frequent.

craig1972

2:09 am on Feb 11, 2008 (gmt 0)

10+ Year Member



Hi. Yes, I backup my files. That's not the issue really. I'm on a dedicated server so all of this procedure is mine. I simply get the service provider to install/upgrade Apache because WHM doesn't really make it fool-proof. Thanks for your thoughts though.

Jd, or anyone else, I have two questions from above:

1. Is putting rules in httpd.conf any better than putting them in individual .htaccess files? In terms of performance efficiencies?

2. In httpd.conf, how can I RewriteRule folders in my website, and not only write rules from the root path?

Thanks!

wilderness

2:41 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Is putting rules in httpd.conf any better than putting them in individual .htaccess files? In terms of performance efficiencies?

Yes.
This has been answered time and again, by Jim.

[webmasterworld.com...]

craig1972

2:55 am on Feb 11, 2008 (gmt 0)

10+ Year Member



Thanks. Yes, I did find some threads that suggested that to derive REAL value out of httpd.conf, we need to set AllowOverride to "None", otherwise Apache still traverses directories for the .htaccess files.

I tried putting that in my post above, but I'm at work and WebmasterWorld site is a little slow from here. Hope this message shows up.

That leaves my question no.2, about RewriteRules that are not from the root path. Thanks for any thoughts!

craig1972

3:39 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Ok, I've answered both my questions.

Firstly, yes the httpd.conf rules are much better in performance because they get compiled at starting time. But to see real benefit, turn "AllowOverride None". In a typical WHM/Cpanel context this needs to be in <Directory> tags inside the VirtualHost definition.

Secondly, the rules I have been writing since my postno.1 above actually already work! I just had an aggressive rule for that entire directory BEFORE these ones, with an [L] instruction so that became the last. In other words, Apache was never getting to these rules in the first place. How stupid I feel - but I'm glad it's now working.

Thanks for your help...or lack of it - which pushed me to try harder. I could have searched around this site but sometimes newbies don't know what keywords to use.

jdMorgan

4:26 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put your rules in order, external redirects first, followed by internal rewrites. Within these groups, the rules should be ordered from most-specific (least 'aggressive' in your terms) to least-specific. Always use the [L] flag unless you know a good reason that you should not use it.

Jim