#1
  1. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128

    Rules in httpd.conf AND .htaccess


    Hey guys, my Google-fu is not strong this night, so a question:

    If you have rewrite rules in both the httpd.conf file (or included vhost file) and also in the document root's .htaccess file, will only one work?

    That's what I'm seeing and I don't understand why.

    On the httpd.conf side I have a simple rule to prepend www. to the domain if it's not already there.

    In the .htaccess file I have a series of "pretty URL" rewrites that translate a string into a series of query string name/values.

    It seems like the .htaccess file is running first (which seems odd) since those rewrites work, but the www. prepend rewrite doesn't.

    Thoughts?
    I'd really like to keep them separate, as a means to separate "hostname configurations" from "application configurations".

    Thanks in advance

    P.S. And yes, I know the www. rewrite is formatted right since it works fine if I put it in the .htaccess file or disable the .htaccess file's rewrites.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    Originally Posted by dmittner
    If you have rewrite rules in both the httpd.conf file (or included vhost file) and also in the document root's .htaccess file, will only one work?
    Both will work, but they can conflict with each other. And in ways you might not suspect.

    What rewriting do you have in the two files?
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by requinix
    Both will work, but they can conflict with each other. And in ways you might not suspect.

    What rewriting do you have in the two files?
    Code:
    vhost file:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
    
    .htaccess:
    RewriteEngine on
    RewriteRule ^f(\d+)t(\d+)p(\d+) /modules/forums/topic/?forumId=$1&topicId=$2&postId=$3 [QSA]
    RewriteRule ^f(\d+)t(\d+)\.(\d+) /modules/forums/topic/?forumId=$1&topicId=$2&navPage=$3 [QSA]
    RewriteRule ^f(\d+)t(\d+) /modules/forums/topic/?forumId=$1&topicId=$2 [QSA]
    RewriteRule ^f(\d+)p(\d+) /modules/forums/topic/?forumId=$1&postId=$2 [QSA]
    RewriteRule ^t(\d+)p(\d+) /modules/forums/topic/?topicId=$1&postId=$2 [QSA]
    RewriteRule ^t(\d+)\.(\d+) /modules/forums/topic/?topicId=$1&navPage=$2 [QSA]
    RewriteRule ^t(\d+) /modules/forums/topic/?topicId=$1 [QSA]
    RewriteRule ^p(\d+) /modules/forums/topic/?postId=$1 [QSA]
    RewriteRule ^f(\d+)\.(\d+) /modules/forums/forum/?forumId=$1&navPage=$2 [QSA]
    RewriteRule ^f(\d+) /modules/forums/forum/?forumId=$1 [QSA]
    RewriteRule ^c(\d+) /modules/forums/?categoryId=$1 [QSA]
    Not much room for collision since one deals with the host, the other with what's after it. And they do work fine when in the same file. It's only when in separate files that .htaccess seems to take precedence.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    Ah, seems I was slightly mistaken. There's a barrier between the main server configuration and virtual hosts.

    I can't find it in the current docs (I think it's slightly changed from 2.2 to 2.4) but http://httpd.apache.org/docs/2.2/mod...te.html#vhosts:
    By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <VirtualHost> section:

    Code:
    RewriteEngine On
    RewriteOptions Inherit
    In my defense I'm not used to putting rules in the httpd.conf - I've always seen them in the virtual host configuration and .htaccess files.
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by requinix
    Ah, seems I was slightly mistaken. There's a barrier between the main server configuration and virtual hosts.

    I can't find it in the current docs (I think it's slightly changed from 2.2 to 2.4) but http://httpd.apache.org/docs/2.2/mod...te.html#vhosts:

    In my defense I'm not used to putting rules in the httpd.conf - I've always seen them in the virtual host configuration and .htaccess files.
    Good info. Putting that in both files overcame that problem, but revealed another one. All in all, not worth the trouble. I'll just keep the www. prepend rewrite in the .htaccess file. Feels dirty, but I have enough battles to fight that this one isn't that important.

    If you're morbidly curious what the next problem was...

    The .htaccess rewrites running first, the URL was being rewritten "internally". Meaning PHP would see conventional query string arguments while the browser would retain the "pretty URL".

    Problem is: when the vhost rewrites ran and performed a redirect to add www. in the browser, it also pushed the "internal" form of the URL into the browser. It functions, but negates the purpose of the prettier URLs.

    Long story short, the www. prepend rewrite HAS to happen first, which means putting it in the .htaccess file before the other rewrites.

    Not a big deal, really. I'm just being a bit anal about keeping the URL pretty since it's news/blog/forum site I hope will have its URLs shared around.

    Thanks for the help! Seems we at least both learned a bit about the inheritance piece.

    In my defense I'm not used to putting rules in the httpd.conf - I've always seen them in the virtual host configuration and .htaccess files.
    Just noticed this piece. I'm actually dealing with vhost entries and not httpd.conf itself. In terms of execution I figured httpd.conf/vhosts would be the same, with .htaccess being the odd man out.

    Plus Googling the problem revealed a lot of people mentioning httpd.conf and nobody mentioning vhosts, so I thought they were synonymous in this context.

    Anyway, it looks like there's still a different between execution of .htaccess and vhosts, so we worked through it the same way, anyhow.
    Last edited by dmittner; August 5th, 2012 at 05:37 PM.

IMN logo majestic logo threadwatch logo seochat tools logo